0

My question is quite similar to others about sort, but I finally didn't find answer, so I'm posting.

After JSON encode, I have got array like this.

 array(2) 
    { [0]=> array(105)
        { [0]=> array(25) 
            { ["id"]=> int(6118) 
            ["region_id"]=> int(2) 
            ["region"]=> string(6) "Region" 
            ["offer_issuer_id"]=> int(1) 
            ["offer_issuer"]=> string(11) "Some issuer" 
            ["profession_id"]=> int(3614) 
            ["profession"]=> string(33) "some profession info" 
            ["position"]=> string(45) "Some position info" 
            ["kind"]=> string(8) "parttime" 
            ["expiration_date"]=> string(10) "2015-08-09" 
            ["url"]=> string(57) "some_url_address" 
            ["created_at"]=> string(24) "2015-07-09T09:57:05.000Z" 
            ["updated_at"]=> string(24) "2015-07-09T09:57:05.000Z"
[...] 
        } 
        [1]=> array(25) 
            { 
            ["id"]=> int(6150) 
            ["region_id"]=> int(2) 
            ["region"]=> string(6) "Region" 
            ["offer_issuer_id"]=> int(1) 
            ["offer_issuer"]=> string(11) "Some issuer" 
            ["profession_id"]=> int(3599) 
            ["profession"]=> string(23) "some profession info" 
            ["position"]=> string(38) "Some position info"
            ["kind"]=> string(8) "parttime" 
            ["expiration_date"]=> string(10) "2015-08-15" 
            ["url"]=> string(57) "some_url_address" 
            ["created_at"]=> string(24) "2015-07-18T11:49:43.000Z" 
            ["updated_at"]=> string(24) "2015-07-18T11:49:43.000Z" 
            [...] 
            } 
        [2]=> array(25) 
            { 
            ["id"]=> int(6165) 
            ["region_id"]=> int(2) 
            ["region"]=> string(6) "region" 
            ["offer_issuer_id"]=> int(1) 
            ["offer_issuer"]=> string(11) "Some issuer" 
            ["profession_id"]=> int(8443) 
            ["profession"]=> string(23) "some profession info" 
            ["position"]=> string(38) "Some position info"
            ["kind"]=> string(8) "parttime" 
            ["expiration_date"]=> string(10) "2015-08-16" 
            ["url"]=> string(57) "some_url" 
            ["created_at"]=> string(24) "2015-07-27T09:53:52.000Z" 
            ["updated_at"]=> string(24) "2015-07-27T09:53:52.000Z" 
    [...] 
            } 

I have to sort this data by date, but everytime I'm trying, I get error

"Notice: Undefined index: created_at in D:\xampp... on line 53

This is some sort code:

function sortFunction ($a, $b){
    return strtotime($a["created_at"]) - strtotime($b["created_at"]);

}
usort($json, "sortFunction");

I think the problem is in return syntax - I can't get into created_at variable. Any tips how to do it?

1
  • Try return strtotime($a[0]["created_at"]) - strtotime($b[0]["created_at"]); Commented Aug 3, 2015 at 13:44

1 Answer 1

1

Your error is on this expression

return strtotime($a["created_at"]) - strtotime($b["created_at"]);

You are dealing with a multi-dimensional array, so you need to specify keys for every dimension.

Based on you JSON dump I can see that you have One array of 2 elements. Indices 0 to 1

Element 0 contains an array of 105 elements. Indices 0 to 104

Each of those Elements are themselves associative arrays of 25 elements each. Distinct string keys.

So In order to access the created_at value that you are looking for, you need to access each of those levels of the head array.

$a[0][0]["created_at"]; //first element
$a[0][1]["created_at"]; //second element
$a[0][2]["created_at"]; //third element
$a[0][3]["created_at"]; //fourth element
...

Will get you the value you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

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.