Sunday, 15 August 2010

php - Sort array matching key order -



php - Sort array matching key order -

i have next arrays:

$names = array('fred', 'bob', 'john'); $input = array('bob' => array('bob data'), 'john' => array('john data'), 'fred' => array('fred data'));

i want re-order keys in $input in exact same order specified in $names. ideas?

seeing how sec array suitably indexed, can treat recombination problem rather thinking in terms of sorting algorithms:

function recombine(array $input, array $names) { $result = []; foreach ($names $name) { $result[$name] = $input[$name]; } homecoming $result; } $input = recombine($input, $names);

if names not nowadays in $input need set status within loop body.

php sorting

No comments:

Post a Comment