循环一个字符串并在PHP中构建一个json数组

时间:2014-05-13 12:31:50

标签: php arrays

我从购物车请求中返回了以下json字符串。

[{"id":19,"name":"Memory","display_name":"Memory","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/19\/values.json","resource":"\/options\/19\/values"}},{"id":20,"name":"Add Keyboard & Mouse","display_name":"Add Keyboard & Mouse","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/20\/values.json","resource":"\/options\/20\/values"}},{"id":21,"name":"Monitor","display_name":"Monitor","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/21\/values.json","resource":"\/options\/21\/values"}},{"id":22,"name":"Operating System","display_name":"Operating System","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/22\/values.json","resource":"\/options\/22\/values"}},{"id":23,"name":"AntiVirus","display_name":"AntiVirus","type":"RB","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/23\/values.json","resource":"\/options\/23\/values"}}]

我需要循环上面的内容并提取id“

的每个值

然后我为每个值调用另一个url。 这将返回一个json字符串(对于id = 19):

[{"id":73,"option_id":19,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":74,"option_id":19,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]

并且id = 20

[{"id":75,"option_id":20,"label":"No (Default)","sort_order":0,"value":"No (Default)"},{"id":76,"option_id":20,"label":"Yes (+$18)","sort_order":1,"value":"Yes (+$18)"}]

我现在需要将所有这些组合起来并作为json数组回显。

所以问题是。 1.如何将第一个转换为数组并循环遍历。 2.将返回的所有字符串合并到一个json数组中。

MrWarby

2 个答案:

答案 0 :(得分:1)

  1. 使用json_decode
  2. 使用json_encode
  3. 如果您的PHP版本较旧且没有这些功能,则上述页面中的注释会引用其他实现。

    约:

    $inJSON = <<<EOF
    [
    {"id":19,"name":"Memory","display_name":"Memory","type":"S",
      "values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/19\/values.json","resource":"\/options\/19\/values"}},
    {"id":20,"name":"Add Keyboard & Mouse","display_name":"Add Keyboard & Mouse","type":"S",
      "values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/20\/values.json","resource":"\/options\/20\/values"}},
    {"id":21,"name":"Monitor","display_name":"Monitor","type":"S",
      "values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/21\/values.json","resource":"\/options\/21\/values"}},
    {"id":22,"name":"Operating System","display_name":"Operating System","type":"S",
      "values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/22\/values.json","resource":"\/options\/22\/values"}},
    {"id":23,"name":"AntiVirus","display_name":"AntiVirus","type":"RB",
      "values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/23\/values.json","resource":"\/options\/23\/values"}}
    ]
    EOF;
    
    $decode = json_decode($inJSON);
    foreach ($decode as $key => $item) {
       echo "Id:" . $item->id . "\n";
    }
    

    顺便说一句,您打算进行的每个URL调用的JSON输出似乎表明它返回一个JSON数组。您是否无法使用您想要发回的所有所需的所有ID值进行单个网址调用?对第二个服务器的多个请求可能会出现性能问题,并且将此减少到一个呼叫将避免将多个响应连接在一起的需要。

    否则你必须

    1. 从你回来的字符串中删除前导和尾随[]
    2. 将所有人加在一起

      $ finalResponse ='['。加入(',',$回复)。 ']';

答案 1 :(得分:1)

    $firstJSONArray = json_decode('[{"id":19,"name":"Memory","display_name":"Memory","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/19\/values.json","resource":"\/options\/19\/values"}},{"id":20,"name":"Add Keyboard & Mouse","display_name":"Add Keyboard & Mouse","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/20\/values.json","resource":"\/options\/20\/values"}},{"id":21,"name":"Monitor","display_name":"Monitor","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/21\/values.json","resource":"\/options\/21\/values"}},{"id":22,"name":"Operating System","display_name":"Operating System","type":"S","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/22\/values.json","resource":"\/options\/22\/values"}},{"id":23,"name":"AntiVirus","display_name":"AntiVirus","type":"RB","values":{"url":"https:\/\/store-v035aw9.mybigcommerce.com\/api\/v2\/options\/23\/values.json","resource":"\/options\/23\/values"}}]');

    $finalArray = array();

    foreach ($firstJSONArray as $record) {
        $itemID = $record->id;

        // get data for each ID from your API like you have given example of 18 and 19 and store the push data to final array

        $finalArray[] = "json_data_from_api_for_particular_id";
    }

    echo json_encode($finalArray);