鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Java  PHP  Linux  網(wǎng)絡(luò)安全/ php 印出 json array一直失敗?

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撈過來以上的數(shù)據(jù)
對方的需求是要告知type

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

$result = curl_exec($ch);

於是我印出 echo $result,取得的結(jié)果是同一個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()

是哪裡有問題?!

回答
編輯回答
淺時光

需要先將$result結(jié)果使用$result = json_decode($result, true);解析為數(shù)組,之后再執(zhí)行如下操作

foreach($result['list'] as $mydata)
{
    echo $mydata['name'];
}
2018年5月3日 03:29