Perad Posted November 10, 2009 Share Posted November 10, 2009 Hi, I have 3 arrays as shown below. $users = $this->get_latest_users(); $flirt = $this->get_latest_flirts(); $hotlist = $this->get_latest_hotlist(); These arrays for a key have the strtotime value of whenever something was posted. They then have some html as a value. So.. $users = array ('1257785194' => 'html'); What I need to do is merge the 3 above arrays and sort them so it shows the soonest first. The structure of arrays can be changed, thats not a problem. I just need to get this working. Any ideas? EDIT: I need to preserve the strtotime value. Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/ Share on other sites More sharing options...
iversonm Posted November 10, 2009 Share Posted November 10, 2009 Please post all three functions get_latest_users(), get_latest_flirts(), get_latest_hotlists(). Also by using $this I am going to assume you are using classes and functions? Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954706 Share on other sites More sharing options...
Perad Posted November 10, 2009 Author Share Posted November 10, 2009 App is OOP. The functions are somewhat irrelevant. Here is the user function. The rest of the functions are identical with different table / row names. public function get_latest_users() { $array = array(); $query = $this->db->from('users')->order_by('id', 'desc')->limit('10')->get(); foreach ($query->result() as $row) { $array[strtotime($row->joined)] = '<tr> <td class="profile_image"> {img} </td> <td class="info"> <h3>A user has registered</h3> </td> </tr>'; } return $array; } Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954708 Share on other sites More sharing options...
iversonm Posted November 10, 2009 Share Posted November 10, 2009 Well I don't know what you have tried bu you could always try this. First array_merge them $Data=array_merge($this->get_latest_users(), $this->get_latest_flirts(), $this->get_latest_hotlists()); Then just sort them. $Sorted=array_multisort($Data, SORT_DESC); or use SORT_ASC if you want opposite direction. Should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954709 Share on other sites More sharing options...
Garethp Posted November 10, 2009 Share Posted November 10, 2009 $New = array(); function Ins($In, $Out); { foreach($In as $k=>$v) { $Out[] = array( 'time'=>$k, 'value'=>$v ); } return $Out; } function cmp($a, $b) { if($a['time'] == $b['time']) { return 0; } else if($a['time'] < $b['time']) { return 1; } else { return -1; } } $New = Ins($users, $New); $New = Ins($flirt, $New); $New = Ins($hotlist, $New); usort($New, "cmp"); foreach($New as $v) { echo "$v<br>"; } Try that. Though it is untested Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954711 Share on other sites More sharing options...
Adam Posted November 10, 2009 Share Posted November 10, 2009 You could do something like this: $users = array ('2546456' => 'html', '4546456' => 'html'); $flirt = array ('3546456' => 'html'); $hotlist = array ('1546456' => 'html'); $merged = $users + $flirt + $hotlist; ksort($merged); print_r($merged); array_merge() wouldn't work here as the keys would be over-written. Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954713 Share on other sites More sharing options...
Garethp Posted November 10, 2009 Share Posted November 10, 2009 Just wondering, when you merge an associative array, what happens if one index exists in more than one array? Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954715 Share on other sites More sharing options...
Adam Posted November 10, 2009 Share Posted November 10, 2009 Just wondering, when you merge an associative array, what happens if one index exists in more than one array? The first key is used I believe. Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954717 Share on other sites More sharing options...
Daniel0 Posted November 10, 2009 Share Posted November 10, 2009 Just wondering, when you merge an associative array, what happens if one index exists in more than one array? The right-most will override the others. Edit: That is for array_merge(). When using + it'll be like MrAdam said. Quote Link to comment https://forums.phpfreaks.com/topic/180952-help-ordering-arrays/#findComment-954718 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.