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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,