I have 2 arrays and need to sort $array1
based on the keys of $array2
. Here are the arrays:
$array1 = array(
'a1' => 'text1',
'a2' => 'text2',
'a3' => 'text3',
'a4' => 'text4'
);
$array2 = array(
'a2' => '1',
'a1' => '1',
'a3' => '0',
'a4' => '0'
);
After sorting, $array1
should look like this.
$array1 = array(
'a2' => 'text2',
'a1' => 'text1',
'a3' => 'text3',
'a4' => 'text4'
);
I do not want to duplicate the array, but want $array1
to be mutated. In addition, sometimes $array2
will not contain all the keys in $array1
.