php 印出 json array一直失敗?

{
    "list": [
        {
            "attribute-id": "f11",
            "name": "love",
            "type": "me"
        },
        {
            "attribute-id": "f12",
            "name": "member",
            "type": "her"
        },
        {
            "attribute-id": "f13",
            "name": "user",
            "type": "her"
        },
        {
            "attribute-id": "f14",
            "name": "like",
            "type": "me"
        }
    ]
}

我利用curl跟別人的後端api撈過來以上的數據
對方的需求是要告知type

$data = array(
  "type" => "skill"
);
$data_string = json_encode($data);

$result = curl_exec($ch);

於是我印出 echo $result,取得的結果是同一個type沒錯

但我想在我這端用php印出來
例如所有的名字

foreach($result->list as $mydata)

    {
         echo $mydata->name;

    }

但這樣卻沒辦法印出
Notice: Trying to get property 'list' of non-object
Warning: Invalid argument supplied for foreach()

是哪裡有問題?!

侃侃尔雅
浏览 349回答 1
1回答

呼唤远方

需要先将$result结果使用$result = json_decode($result, true);解析为数组,之后再执行如下操作 foreach($result['list'] as $mydata) { echo $mydata['name']; }
打开App,查看更多内容
随时随地看视频慕课网APP