There are two Arrays: {code ...} is there any built-in function in php that can sort $ array1 by $ array2 in the following format: {code ...} I have read the array_multisort () documentation, but it does not seem to support it. There are two Arrays:
$ Aray1 = ('key3' => 'value3 ', 'key2' => 'value2', 'key1' => 'value1); $ aray2 = ('key1 ', 'key3', 'key2'); // The sequence is not 123.
Is there any built-in function in php that can sort $ array1 by $ array2 in the following format:
$aray1 = ( 'key1' => 'value1', 'key3' => 'value3', 'key2' => 'value2);
I have read the array_multisort () documentation, but it does not seem to support it.
Reply content:
There are two Arrays:
$ Aray1 = ('key3' => 'value3 ', 'key2' => 'value2', 'key1' => 'value1); $ aray2 = ('key1 ', 'key3', 'key2'); // The sequence is not 123.
Is there any built-in function in php that can sort $ array1 by $ array2 in the following format:
$aray1 = ( 'key1' => 'value1', 'key3' => 'value3', 'key2' => 'value2);
I have read the array_multisort () documentation, but it does not seem to support it.
It seems that no. array_multisort () mainly sorts multi-dimensional arrays.
Use the User-Defined sorting function usort (), but you still need to write it yourself.
foreach($array2 as $i => $key) { $result[$key] = $array1[$key];}var_dump($result);
$data = array();array_walk($aray2,function($value, $key) use($aray1, &$data){ $data[$value] = $aray1[$value]; });print_r($data);
Based on my experience, there is no such internal function
You can write a method by yourself.
You can write a method by yourself.
Write it yourself
Only write one according to the sorting rules.