Reputation: 20726
I have 2 Arrays:
(1) Array with Key => Value, and Array (2) with no relevant Keys and as Values the keys from array 1 in different order. Is there an elegant solution to put array 1 in the same order like the values in array two?
$data = array(
'NAME' => 'XYZ',
'NUMB' => 1234,
'CITY' => 'TEST'
);
$sort = array(
'A1' => 'CITY',
'XY' => 'NUMB',
'XX' => 'NAME',
);
$result = array(
'CITY' => 'TEST,
'NUMB' => 1234,
'NAME' => 'XYZ',
);
Upvotes: 0
Views: 89
Reputation: 590
I noticed the typo, and think you want this:
$result=array();
foreach ($sort as $var=>$val){
$result[$val]=$data[$val];
}
Upvotes: 2
Reputation: 10246
$data = array(
'NAME' => 'XYZ',
'NUMB' => 1234,
'CITY' => 'TEST'
);
$sort = array(
'A1' => 'CITY',
'XY' => 'NUMB',
'XX' => 'NAME'
);
$result = array();
foreach($sort as $key => $value){
if(isset($data[$value]))
$result[$value] = $data[$value];
}
print_r($result);
Upvotes: 2