douzhan1963 2014-04-02 16:18
浏览 40
已采纳

在php中回显一个数组

I am creating the following object from the bigCommerce api as follows:

[
{
    "id": 412,
    "option_id": 37,
    "display_name": "testSteveMemory",
    "sort_order": 0,
    "is_required": true
},
{
    "id": 413,
    "option_id": 34,
    "display_name": "Hard Drive (desktop)",
    "sort_order": 1,
    "is_required": true
},
{
    "id": 414,
    "option_id": 24,
    "display_name": "Include Keyboard & Mouse",
    "sort_order": 2,
    "is_required": true
},
{
    "id": 415,
    "option_id": 33,
    "display_name": "Memory",
    "sort_order": 3,
    "is_required": true
}
]

I convert this to a PHP array using :

$curlProductOptions = json_decode($curlProductOptions, true);

I then loop through the array and get the option for that option_id

$allOptions = array();  
foreach($curlProductOptions as $value){
    //echo $value['option_id'].'<br>';
    $option_id = $value['option_id'];


$product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $product_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlProductData = curl_exec($curl);
array_push($allOptions,$curlProductData);
curl_close($curl);
}

I am then using echo to output the array $allOptions and I consume this in a mobile applications as json. if I use

echo $curlProductData.'<br>' 

I get the following:

[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}]
[{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}]
[{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}]
[{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]

If I use echo any of the following:

echo $allOptions;
json_encode($allOptions);
var_dump($allOptions);

I get a parse error when the data is returned to the mobile app. ie it is not recognising the result as a json array.

I am presuming:

  1. That $allOptions = array() declares $allOptions as an array

  2. I am populating $allOptions with each $curlProductData in the loop.

What am I doing wrong here ?

I am now using :

echo "[".implode(",
",$allOptions)."]";

I am now getting the following returned :

[[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}], [{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}], [{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}], [{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]]

but I cannot get the mapping correct ?

MrWarby

  • 写回答

4条回答 默认 最新

  • dony39517 2014-04-02 17:01
    关注

    The following solution puts each of the items in a single array:

    foreach($curlProductOptions as $value){
        //echo $value['option_id'].'<br>';
        $option_id = $value['option_id'];
    
    
        $product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $product_url);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_ENCODING, "");
        $curlProductData = curl_exec($curl);
    
        foreach(json_decode($curlProductData, true) as $key => $value) {
            $allOptions[] = $value; // [] is quicker than array_push() for single items
        }
    
        curl_close($curl);
    }
    

    If you need your four groupings as in the question then:

    The following solution puts each of the items in a single array:

    foreach($curlProductOptions as $value){
        //echo $value['option_id'].'<br>';
        $option_id = $value['option_id'];
    
    
        $product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $product_url);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_ENCODING, "");
        $curlProductData = curl_exec($curl);
    
        $allOptions[] = json_decode($curlProductData, true);
    
        curl_close($curl);
    }
    

    Now json_encode($allOptions) will give you a valid json String. The problem was you were trying to pass 4 json strings when only 1 was supposed to be read.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题