- var_dump: documentation ( source)
- array_intersect_key: documentation ( source)
- array_merge: documentation ( source)
- array_flip: documentation ( source)
<?php
function algorithm_a()
{
$data = [
'city' => 'Lexington',
'name' => 'Mike Tyson',
'occupation' => 'Hot Dog Vendor',
'state' => 'KY',
'address' => '1009 Nicholasville Road',
'ZIP' => '40526'
];
$order = [
'name',
'city',
'state'
];
$filtered = array_intersect_key(
$data,
array_flip($order)
);
$ordered = array_merge(
array_flip($order),
$filtered
);
return $ordered;
}
$result = algorithm_a();
var_dump($result);