2016-09-14 131 views
0

json解码后我有这个输出。解析JSON数组PHP

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 
        ) 

       ) 

     ) 

    ) 

我需要做一个字符串将值放在iframe中。它给我没有结果 但如果我把报价放在json字符串上,它会给我所需的结果。

$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++;} 

它给了我PHP的非对象解析错误。如何在

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

回答

1

解决你的问题会是这样的形式获取值从PRODUCTLIST,

$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; 

你预计产量将

ITEM1=TEST_PRO_01-Pink&AMT1=135.00&QTY1=1&ITEM2=TEST_PRO_01-Pink&AMT2=0.00&QTY2=1 
+0

我已经试了一下,给我解析错误 –

+0

你可以在这里发布json字符串 – Sasikumar

+0

cj-helper {“CJ”:{“CID”:“1234”,“TYPE”:“type2222”,“CURRENCY”:“GBP”,“OID” “100000045”, “折扣”: “0.00”, “PRODUCTLIST”:[{ “ITEM”:“T EST_PRO_01粉红 “ ”数量“: ”1“, ”AMT“: ”135.00“, ”DCNT“: ”0.00“},{ ”ITEM“: ”TEST_PRO_01粉红“, ”数量“: ”1“,” AMT“:”0.00“,”DCNT“:”0.00“}]}} –