0

I have this output after json decode.

Array
      (
         [CJ] => stdClass Object
            (
              [CID] => 1234 
              [TYPE] => type2222
              [CURRENCY] => GBP
              [OID] => 100000037
              [DISCOUNT] => 0.00
                [PRODUCTLIST] => Array
                  (
                    [0] => stdClass Object
                       (
                        [ITEM] => TEST_PRO_02-White-M
                        [QTY] => 2
                        [AMT] => 80.00
                        [DCNT] => 0.00
                       )

                  [1] => stdClass Object
                       (
                        [ITEM] => TEST_PRO_02-White-M
                        [QTY] => 2
                        [AMT] => 0.00
                        [DCNT] => 0.00
                      )

                   [2] => stdClass Object
                      (
                        [ITEM] => TEST_PRO_01-Pink
                        [QTY] => 1
                        [AMT] => 135.00
                        [DCNT] => 0.00
                      )

                   [3] => stdClass Object
                      (
                        [ITEM] => TEST_PRO_01-Pink
                        [QTY] => 1
                        [AMT] => 0.00
                        [DCNT] => 0.00
                      )

               )

         )

   )

I need to make a string to put values in iframe. it gives me no result but if i put quotes on json string it gives me required result.

$x=0;
foreach ($obj->CJ->PRODUCTLIST as $productlist){


 $item=$productlist->ITEM;
 $amount=$productlist->AMT;
 $qty=$productlist->QTY;
 $cj_string.="ITEM".$x."=$item&AMT".$x."=$amount&QTY".$x."=$qty&";
$x++;}

it gives me php nonobject parsing error. How do I get values from PRODUCTLIST in the form of

item1=value&QTY1=value&AMT1=value&item2=value&QTY2=value&AMT2=value

1 Answer 1

1

Solution for your question would be this,

$cjhelper = '{"CJ":{"CID":"1234","TYPE":"type2222","CURRENCY":"GBP","OID":"100000045","DISCOUNT":"0.00","PRODUCTLIST":[{"ITEM":"TEST_PRO_01-Pink","QTY":"1","AMT":"135.00","DCNT":"0.00"},{"ITEM":"TEST_PRO_01-Pink","QTY":"1","AMT":"0.00","DCNT":"0.00"}]}}';
$obj = json_decode($cjhelper);

$x=1;
$cj_string = "";
  foreach ($obj->CJ->PRODUCTLIST as $productlist){
     $item=$productlist->ITEM;
     $amount=$productlist->AMT;
     $qty=$productlist->QTY;
      if($x!=1) $cj_string.= "&";
     $cj_string.="ITEM".$x."=$item&AMT".$x."=$amount&QTY".$x."=$qty";
    $x++;
    }

echo $cj_string;

Your expected output will be

ITEM1=TEST_PRO_01-Pink&AMT1=135.00&QTY1=1&ITEM2=TEST_PRO_01-Pink&AMT2=0.00&QTY2=1
Sign up to request clarification or add additional context in comments.

4 Comments

can you post the json string here
cj-helper{"CJ":{"CID":"1234","TYPE":"type2222","CURRENCY":"GBP","OID":"100000045","DISCOUNT":"0.00","PRODUCTLIST":[{"ITEM":"TEST_PRO_01-Pink","QTY":"1","AMT":"135.00","DCNT":"0.00"},{"ITEM":"TEST_PRO_01-Pink","QTY":"1","AMT":"0.00","DCNT":"0.00"}]}}
When I write your code as it is, it gives me required result. so basically my json string is missing single quotes. problem in json encoding?
Where did i went wrong JSON string was made by JSON encoding with dynamic populated values. how i put these singlw quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.