0

I have one array which have the right order of the keys, For example,

$array_keysorder=([0]=>"Fire",[1]=>"Sky",[2]=>"Third")

//Array i want to sort,after appending the order is messed up
$tosortarray=(['Sky']=>array(array()...),['Third']=>array(),['Fire']=>array())

//This is how i want the final array to look like 
$Final=(['Fire']=>array(), ['Sky']=>array(array()...),['Third']=>array())
4

1 回答 1

0

重建阵列呢?

编辑..我添加了一些评论..

<?php
$array_keysorder[] = "Fire";
$array_keysorder[] = "Sky";
$array_keysorder[] = "Third";

print_r($array_keysorder);

$bad_array[Sky] = "data1";
$bad_array[Third] = "data2";
$bad_array[Fire] = "data3";

echo "<br>";
print_r($bad_array);

//This count cycles through the sort order, 0 = Fire, 1 = Sky, etc.
$key = 0;

//Cycle through array. $row isn't used.
foreach($bad_array as $row)
{
    //$temp_val will store the key name ie "Fire".
    $temp_val = $array_keysorder[$key];
    //Create a organized array with the correct order of the keys
    $good_array[$temp_val] = $bad_array[$temp_val];
    //Increase the key to the next one.
    $key++;
}

echo "<br>";
print_r($good_array);
?>
于 2012-07-18T01:26:14.583 回答