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?
return strtotime($a[0]["created_at"]) - strtotime($b[0]["created_at"]);