从两个不同来源排序数据(PHP)

I need to combine two different data types, an array and an array object.

I then need to display them on a page in order of a certain attribute (date).

The markup for access is similar to the following:

foreach($array as $item){
$item['date'];
}

and

foreach($object as $item){
$item->post->date
}

is array_merge what I need, or something different?

Not that if possible I'd like to do this on the fly, as data will be changing rapidly and there is no need for storage.

Thanks!

Here's how I would do it:

// array we will use for sorting
$finalArray = array();
// add the array's using the date as the key
foreach($array as $item){
     $key = $item['date']; // use date here, example $key = date('l \t\h\e jS',$item['date']);
     $finalArray[$key] = $item;
}
// add the objects's using the date as the key
foreach($object as $item){
     $finalArray[$item->post->date] = $item;
}
//now sort by keys as Xeoncross noted
ksort($finalArray);
foreach($finalArray as $date=>$objOrArray){
     if(is_array($objOrArray)){
          //do your array printing here
     } else {
          //do your object printing here
     }
}

Ofcourse we can turn the object into an array with get_object_vars, and use whatever sorting function on the final array, the important part is that we want to sort by date and that's why we need it to be our key.

Hope that helped.

$dates = array();

foreach ($array as $item) {
  $dates[] = $item['date'];
}


foreach ($object as $item) {
    $dates[] = $item->post->date;
}

sort($dates);

foreach ($dates as $date) {
   echo $date;
}

You could try this if you need multiple values from the objects (not just date) and you don't mind duplicates being erased.

// $array is already defined right?
$object = json_decode(json_encode($object), TRUE);
$data = array_merge($array, $object);

print_r($data); // now test it

http://us2.php.net/array_merge
http://us3.php.net/json_decode (note the second TRUE param)

Edit

Based on Perfection's answer, (and re-reading the question) I would do this:

$finalArray = array();
foreach($array as $item)
{
     $finalArray[$item['date']] = $item;
}

foreach($object as $item)
{
     $finalArray[$item->post->date] = json_decode(json_encode($item), TRUE);
}

ksort($finalArray);

foreach($finalArray as $date => $item)
{
    // Everything is an array now
}
foreach($array as $item){
 $array_new[] = $item['date'];
 }

 foreach($object as $item){
  $array_new[] = $item->post->date;
 }

 sort($array_new);