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?

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?

 

That makes the order non-alphabetical. Are you just trying to reverse the order of the original array?

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:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.