1

I am very new to JSON and i really need help. So I have been given this $oldAuctions object and after using PHP's json_decode function (json_decode($oldAuctions, TRUE);) it returned me something like this that has 11 objects with 9 set of name/value pairs in each...

{
    "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"
        }
    ]
}

Now I need to grab each pair and save their values in an array. For example, I want to grab the auction_id and save it's values in an array.....how can I do that?

Also, just for simple testing purpose I tried to print out the values first...but that didn't work either...

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

I would really appreciate your help. Thank you!

5
  • 2
    What you posted looks like the JSON object before decoding it, not what json_decode() returned. Commented Jan 1, 2013 at 4:49
  • Oh, and my PHP is NOT the 5.3 version. Thanks! Commented Jan 1, 2013 at 4:49
  • What does $oldAuctions contain? JSON string or Array? Commented Jan 1, 2013 at 4:52
  • @Barmar: oops, u r right. i actually used json_encode Commented Jan 1, 2013 at 4:56
  • @shiplu.mokadd.im: i believe it's an array (what i posted in my question) Commented Jan 1, 2013 at 4:58

1 Answer 1

2

After invoking json_encode your array $oldAuctions has become a JSON string. You can not iterate through it as array or object as its a string.

As $oldAuctions is already an array you can simply use forach

foreach($oldAuctions['recent_results'] as $result){
    echo 'Ids: '.$result['auction_id']. "\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

AH! i see :) Thank you! another silly question, how to save them in an array now?
$new_auctions[] = $result['auction_id']; will add the auction ID to the $new_auctions array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.