1

我对 JSON 很陌生,我真的需要帮助。所以我得到了这个 $oldAuctions 对象,在使用 PHP 的 json_decode 函数 ( json_decode($oldAuctions, TRUE);) 之后,它返回了类似这样的东西,其中有 11 个对象,每个对象有 9 组名称/值对...

{
    "recent_results": [
        {
            "closing_yield": "0.800",
            "auction_id": "106",
            "use_buy_it": "0",
            "issuer": "National Bank",
            "term": "1 Year",
            "is_charity": "1",
            "end_date": "12-26-2012",
            "closing_price": "100.000000",
            "issue_type": "FDICs"
        },
        {
            "closing_yield": "1.090",
            "auction_id": "339",
            "use_buy_it": "0",
            "issuer": "National Bank",
            "term": "1 Year",
            "is_charity": "1",
            "end_date": "12-12-2012",
            "closing_price": "100.000000",
            "issue_type": "FDICs"
        },
        {
            "closing_yield": "2.000",
            "auction_id": "041",
            "use_buy_it": "0",
            "issuer": "National Bank",
            "term": "5 Year",
            "is_charity": "1",
            "end_date": "09-11-2012",
            "closing_price": "100.000000",
            "issue_type": "FDICs"
        }
    ]
}

现在我需要抓取每一对并将它们的值保存在一个数组中。例如,我想获取auction_id并将它的值保存在一个数组中......我该怎么做?

另外,只是为了简单的测试目的,我尝试先打印出这些值……但这也不起作用……

foreach($oldAuctions as $IDs)       
{
   echo 'Ids: '.$IDs->auction_id;
}

我将衷心感谢您的帮助。谢谢!

4

1 回答 1

2

调用后json_encode你的数组$oldAuctions变成了一个 JSON字符串。您不能将其作为数组或对象作为字符串进行迭代。

因为$oldAuctions已经是一个数组,您可以简单地使用forach

foreach($oldAuctions['recent_results'] as $result){
    echo 'Ids: '.$result['auction_id']. "\n";
}
于 2013-01-01T04:58:11.517 回答