0

I have three object values to merge like

$videos = $this->Blogmodel>all_video_posts_ByCategory($cat_id);
$texts = $this->Blogmodel->all_text_posts_ByCategory($cat_id);
$audio = $this->Blogmodel->all_audio_posts_ByCategory($cat_id);

$data['array'] = array_merge($videos, $texts, $audio);

But the problem was values are sorted by one by one .. How to sort by date while merging ??

  • How are we supposed to know what the contents of your arrays are? You need to show more code. Also, you don't show any attempt to sort. What are you doing besides nothing? – Brian Gottier Dec 15 '17 at 05:18
  • Am getting text_interviews and video interviews and audio interviews by perticular one category . Three of them are in different table . so i take indiduvally the i merged .. It shows all videos interviews first then text interviews then audio interviews . But i need to sort by uploaded date .. i have a column name called posted_date at both three table .. How to do that – Gopinath Parthee Dec 15 '17 at 05:22
  • Show your array contents @GopinathParthee – Naim Malek Dec 15 '17 at 05:36
  • text: Array ( [0] => stdClass Object ( [text_id] => 1 [text_cat_id] => 1 [text_title] => Sample [text_description] => Sample Text [interview_type] => 3 [posted_at] => 2017-12-08 13:10:59 ) ) – Gopinath Parthee Dec 15 '17 at 06:22
  • video: Array ( [0] => stdClass Object ( [video_id] => 7 [video_cat_id] => 3 [video_title] => sdgsghfr [video_description] => esyhhttrsgxfgbdzbhfz [youtube_id] => https://www.youtube.com/watch?v=kduLmg9CXl8 [interview_type] => 2 [posted_at] => 2017-11-29 15:50:00 ) – Gopinath Parthee Dec 15 '17 at 06:23

1 Answers1

0

You can first merge the arrays and then sort the final array. You are probably looking for a multi-sort function. See below code:

$data = array_qsort2($data['array'], 'posted_date', "ASC");

function array_qsort2 (&$array, $column=0, $order="ASC") {
    $oper = ($order == "ASC")?">":"<";
    if(!is_array($array)) return;
    usort($array, create_function('$a,$b',"return (\$a['$column'] $oper \$b['$column']);")); 
    return ($array);
}
Patrick R
  • 6,621
  • 1
  • 24
  • 27