PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 8th, 2016, 06:04 PM
mapg mapg is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 mapg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 0
Question General - How to keep original Json format after deleting ID using PHP

Hi all,

I have a function to delete a PID inside a Json string. This function works fine, but when I encode in Json format the resulting string again (using json_encode), the original Json format is lost.

How to keep this original format. That's the question. I need help to do it.

This is the code:
Code:
function deletePID($idToRemove, $dataBase) {
    if (!empty($dataBase)) {
        $dataArray = json_decode($dataBase, true);
        if (is_array($dataArray)) {
            if (is_numeric($idToRemove)) {
                for ($i = 0; $i < count($dataArray['cP']); $i++) {
                    $thisChannel = $dataArray['cP'][$i]['cID'];
                    if ($idToRemove == $thisChannel) {
                        unset($dataArray['cP'][$i]);
                    }
                }
                $thisJason = json_encode($dataArray);
                return $thisJason;
            }
        }
    }
}

$database = '{"cP":[{"cID":"1","PID":"30144"},{"cID":"2","PID":"30147"},{"cID":"3","PID":"30150"}]}';

$pidToDelete = 2;

echo deletePID($pidToDelete, $database);

/*
Output is:

{"cP":{"0":{"cID":"1","PID":"30144"},"2":{"cID":"3","PID":"30150"}}}

instead of ...

{"cP":[{"cID":"1","PID":"30144"},{"cID":"3","PID":"30150"}]}

*/

Any help is really helpful and welcome.

Best,
Mapg

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > General - How to keep original Json format after deleting ID using PHP


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap