Jump to content

Associative array sorting and then matching


Aeterna

Recommended Posts

Hi all,

 

I'm trying to sort my array, which is something along the lines of:

$myArray = array('Test' => 120, 'Tust' => 394, 'Lap' => 439493);

 

What I'm trying to do, is sort the keys (which I've done), and then add the names (Test, Tust, Lap) to an array in the order the keys ordered them, so that I can display them in the order they belong in (the order the keys put them in).  I can't figure it out for the life of me... here's what I'm currently using:

function associative_push($arr, $tmp) {
if (is_array($tmp)) {
	foreach ($tmp as $key => $value) { 
		$arr[$key] = $value;
	}
	return $arr;
}
return false;
}

$participants = array('Lap', 'Test', 'Tust');
$starting = array();
$current = array();
for ($a = 0; $a < count($participants); $a++) {
$starting = associative_push($starting, array($participants[$a] => "testing: $a"));
}
for ($b = 0; $b < count($participants); $b++) {
$current = associative_push($current, array($participants[$b] => "testing: $b"));
}
arsort($starting);
arsort($current);
for ($i = 0; $i < count($participants); $i++) {
$startingExperience = $starting[$participants[$i]];
$currentExperience = $current[$participants[$i]];
// table stuff here (bunch of unnecessary code)
}

 

I'm trying to get it so that 'Lap' would be the first table entry, 'Tust' would be the second, and 'Test' the third.  Any ideas?

Link to comment
Share on other sites

From what you said, you don't want the keys in alphabetical order here?

My thoughts were to use ksort here and store the key into an array...not sure what exactly you are trying to do here.

 

$myArray = array('Test' => 120, 'Tust' => 394, 'Lap' => 439493);
ksort($myArray,SORT_REGULAR);
foreach($myArray as $key => $value){
      $arr[$key] = $value;
}

 

:shrug:

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.