将JSON对象的对保存在数组中

时间:2013-01-01 04:43:48

标签: php json

我是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;
}

我真的很感谢你的帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

调用json_encode后,您的数组$oldAuctions已成为JSON 字符串。你不能将它作为数组或对象迭代它作为一个字符串。

由于$oldAuctions已经是一个数组,您只需使用forach

即可
foreach($oldAuctions['recent_results'] as $result){
    echo 'Ids: '.$result['auction_id']. "\n";
}