Jump to content

Help Ordering Arrays


Perad

Recommended Posts

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.