I have a json object and I wanted to access it in the loop using php. Is there a way I can access it in the loop without even decoding it? Please help. Here is my code.

for($i = 0; $i < count($purchases); $i++){ ?>   

    <tr class="tr-purchase">

      <td>{{ $purchases[$i]['purchase_orders'] }}</td>

    </tr>
}?>

{"id":35,"po_code":"PMS201635","purchase_orders":"a:2:{i:0;a:7:{s:2:\"id\";s:1:\"9\";s:3:\"qty\";s:1:\"2\";s:4:\"unit\";s:7:\"Capsule\";s:6:\"pharma\";s:12:\"Ibuprofen IB\";s:9:\"packaging\";s:7:\"50 caps\";s:5:\"price\";s:2:\"23\";s:5:\"total\";s:2:\"46\";}i:1;a:7:{s:2:\"id\";s:2:\"11\";s:3:\"qty\";s:1:\"5\";s:4:\"unit\";s:6:\"Pieces\";s:6:\"pharma\";s:12:\"Ecotrin oral\";s:9:\"packaging\";s:6:\"100pcs\";s:5:\"price\";s:2:\"13\";s:5:\"total\";s:2:\"65\";}}","freight_charge":"0","overall_total":"111","created_at":"2016-05-21 16:50:49","updated_at":"2016-05-21 16:50:49","shipped_via":"Select","terms":""}

I just wanted to access some values inside the object. How can I access it? Thank you.

share|improve this question
    
if it is json then use json_decode and access it as array. – Frayne Konok May 29 '16 at 8:04
    
POST you example json data, not image. – Frayne Konok May 29 '16 at 8:05
    
Okey what data you need as output? – Frayne Konok May 29 '16 at 8:07
    
In your purchase_orders its also another json, Specify what data you need? – Frayne Konok May 29 '16 at 8:09
    
I just wanted to get the "pharma\";s:12:\"Ibuprofen and get another of the same value in the loop – Eli May 29 '16 at 8:10
up vote 2 down vote accepted

Here is the example how can you got the pharma, if you need more help than please let me know. Online Check

First you need to json_deocde the json string and for getting the purchase_orders its another serialize data, use unserialize and get the array again and access it.

$json = '{"id":35,"po_code":"PMS201635","purchase_orders":"a:2:{i:0;a:7:{s:2:\\"id\\";s:1:\\"9\\";s:3:\\"qty\\";s:1:\\"2\\";s:4:\\"unit\\";s:7:\\"Capsule\\";s:6:\\"pharma\\";s:12:\\"Ibuprofen IB\\";s:9:\\"packaging\\";s:7:\\"50 caps\\";s:5:\\"price\\";s:2:\\"23\\";s:5:\\"total\\";s:2:\\"46\\";}i:1;a:7:{s:2:\\"id\\";s:2:\\"11\\";s:3:\\"qty\\";s:1:\\"5\\";s:4:\\"unit\\";s:6:\\"Pieces\\";s:6:\\"pharma\\";s:12:\\"Ecotrin oral\\";s:9:\\"packaging\\";s:6:\\"100pcs\\";s:5:\\"price\\";s:2:\\"13\\";s:5:\\"total\\";s:2:\\"65\\";}}","freight_charge":"0","overall_total":"111","created_at":"2016-05-21 16:50:49","updated_at":"2016-05-21 16:50:49","shipped_via":"Select","terms":""}';
$result = json_decode ($json);
$arr = unserialize ($result->purchase_orders);

foreach($arr as $val){
    echo '<pre>';
    print_r($val);
}

Specifying the value, lets cehck for the pharma is Ibuprofen IB or not, did this using the if condition.

if($val['pharma'] == 'Ibuprofen IB'){
    echo 'Yes you are in "Ibuprofen IB"';
}
share|improve this answer
    
purchase_orders is Serialized... – Poiz May 29 '16 at 8:33
    
yes, It is serialized data. – Frayne Konok May 29 '16 at 8:35
    
Plus the Content of the Serialized Object is a Multidimensional Array as you can guess from its Signature... – Poiz May 29 '16 at 8:42

First you have to convert the JSON Data back into Native PHP Object by calling "json_decode($jsonData)". The Result will be a PHP Object like so:

    <?php
        // CREATE AN ARRAY TO HOLD THE REAL PHP OBJECT 
        // DECODED FROM THE JSON DATA
        $arrPurchases      = json_decode($purchases);
    ?>

Now you have all the Purchases in an array. The value of each single Key corresponds to a PHP Object which you can now access via a new Loop like so...

    <?php
        foreach($arrPurchases as $i=>$objPurchase){ ?>   

        <tr class="tr-purchase">
            // SINCE purchase_orders IS A SERIALIZED ARRAY
            // YOU STILL HAVE TO UNSERIALIZE IT TO GET THE DATA YOU WANT....
            // BUT YOU MAY JUST var_dump() IT TO SEE THAT YOU ARE ON TRACK...
            <?php var_dump($objPurchase->purchase_orders); ?>

            <!-- UNSERIALIZE THE DATA TO GET WHAT YOU NEED -->
            <?php $data  = unserialize($objPurchase->purchase_orders)); ?>

            <?php // RESULT OF THE UNSERIALIZE WILL BE AN ARRAY... ?>
            <?php $output = ""; foreach($data as $iDex=>$arrObj): ?>
                <td>
                    <!-- START WORKING WITH THE ARRAY: $arrObj -->
                    <!-- BUILD UP YOUR STRING OUTPUT THEREFROM & THE ECHO IT WITHIN THE <TD> -->
                    <?php //$output .= $arrObj[]; ?>
                    <?php // ECHO THE RESULTING OUTPUT HERE; ?>
                </td>
            <?php endforeach; ?>
        </tr>

     <?php }?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.