Reputation: 89
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
Upvotes: 0
Views: 72
Reputation: 863
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
Upvotes: 1