0

キーの正しい順序を持つ配列が 1 つあります。たとえば、

$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 に答える