2

Can someone help me please ... I've read previous PHP 'sorting' questions ... and tried to follow the logic ... but brain fade prevails ;-!

I have a json object (from facebook) which I am trying to sort using one of the values ('created_time') using the following code:

$json = file_get_contents($url,0,null,null);
$result = json_decode($json, true);
array_multisort($note['created_time'], SORT_ASC, $result);   /* what's wrong with this line?? */

Example json data shown below:

{
   "data": [
      {
         "id": "123",
         "from": {
            "name": "Example",
            "category": "Musician/band",
            "id": "123"
         },
         "subject": "Subject 1",
         "message": "Example message text 1",
         "created_time": "2011-07-12T20:18:17+0000",
      },
      {
         "id": "123",
         "from": {
            "name": "Example",
            "category": "Musician/band",
            "id": "123"
         },
         "subject": "Subject 2",
         "message": "Example message text 2",
         "created_time": "2011-07-12T20:21:01+0000",
      },
...

Thanks for you help.

2 Answers 2

2

Your PHP snippet references a $note object but I just see the $json and $result variables...

Assuming we have the data array available you can use usort with a custom function.. this could be an anonymous function if you are using PHP 5.3+

function sort_by_creation($a, $b) 
{
  return (strtotime($a['created_time']) < strtotime($b['created_time']) ? -1 : 1;
}
usort($data, 'sort_by_creation');
Sign up to request clarification or add additional context in comments.

1 Comment

Whoops - the reference to "$note" is completely spurious/wrong - it come from me playing/trying various things in an attempt to debug what's happening.
0

I'm not sure if that will work. Where does $note come from? If you are trying to sort an array by a specific member, what about usort? http://php.net/manual/en/function.usort.php

so something like:

function sortByCreatedTime($a, $b){
  if ($strtotime($a['data']['created_time']) == strtotime($b['data']['created_time'])) {
    return 0;
  };
  return ($strtotime($a['data']['created_time']) < strtotime($b['data']['created_time'])) ? -1 : 1;
};
usort($result, sortByCreatedTime);
for($i = 0; $i < count($results['data']); $i++){
  //should output in descending order.  if not just change 
  //the < to a > in the sort function above
  echo 'time is => ' . $results['data']['created_time'] . '<br>';
};

2 Comments

Thanks for your help @Jake & @thescientist, but I havn't been able to get either of your suggestions to work for me ... Could you try again, now that I've admitted that '$note' was completely spurious. To recap - my array is called $result and the data within it is caled 'data' - and within that the field I need to sort is called 'created_time' Thanks again.
hey, forget to take into consideration the inner 'data' array. see if the above edit helps. not sure which way you needed it sorted, ASC vs. DESC, but as noted in the comments, it's just a switch of the gt/lt sign.

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.