0
{"query":
{"data":{
"item":[{"title":"some word1",
"date":"Sat, 26 Feb 2011 21:02:01"},
{"title":"some word2",
"date":"Sat, 26 Feb 2011 17:02:01"}]
}}}


{"query":
{"text":{
"body":[{"title":"some word3",
"time":"Sat, 26 Feb 2011 20:22:21"},
{"title":"some word4",
"time":"Sat, 26 Feb 2011 19:11:59"}]
}}}

There have 2 json data, how to combine them and echo a result which is order by date? I need a result like:

some word1 Sat, 26 Feb 2011 21:02:01
some word3 Sat, 26 Feb 2011 20:22:21
some word4 Sat, 26 Feb 2011 19:11:59
some word2 Sat, 26 Feb 2011 17:02:01

Thanks

3 Answers 3

2

Use json_decode to decode the json string into an array and then sort the array using any sorting algorithm.

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

2 Comments

Don't forget you should use array_merge to merge the 2 arrays!
do you have a demo or tutorial like this? Thanks.
0

You will have to do some work to combine them. To get a sort result like you provided you need to combine the "item" array of the second json string with the array "body" of the first one. To compare the dates be aware that the fields of the two json string have two different name: "time" and "date", this has to be taken care of in your sort function.

The structures a bit better reabable:

{
   "query":{
      "data":{
         "item":[
            {
               "title":"some word1",
               "date":"Sat, 26 Feb 2011 21:02:01"
            },
            {
               "title":"some word2",
               "date":"Sat, 26 Feb 2011 17:02:01"
            }
         ]
      }
   }
}


{
   "query":{
      "text":{
         "body":[
            {
               "title":"some word3",
               "time":"Sat, 26 Feb 2011 20:22:21"
            },
            {
               "title":"some word4",
               "time":"Sat, 26 Feb 2011 19:11:59"
            }
         ]
      }
   }
}

2 Comments

PHP has a json_decode function that will do all the work for you.
Of course, but it does not magically do the sorting which the OP requested.
0

Something like this?

<?php
$data = array();

// Parse the json into a clean array
$json = json_decode('{"query": 
    {"data":
    { "item":[{"title":"some word1", "date":"Sat, 26 Feb 2011 21:02:01"},
    {"title":"some word2", "date":"Sat, 26 Feb 2011 17:02:01"}] }}} ');

foreach($json->query->data->item as $body){
    $data[strtotime($body->date)][] = $body->title;
}

$json = json_decode(' {"query": 
{"text":
{ "body":[
{"title":"some word3", "time":"Sat, 26 Feb 2011 20:22:21"}, 
{"title":"some word4", "time":"Sat, 26 Feb 2011 19:11:59"}] }}}');

foreach($json->query->text->body as $body){
    $data[strtotime($body->time)][] = $body->title;
}

// Sort the timestamps
ksort($data,SORT_NUMERIC);

// Display the data any way you want
foreach($data as $timestamp => $titles){
    foreach($titles as $title){
        echo $title, ' ', date('r',$timestamp), PHP_EOL;
    }
}

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.