[ create a new paste ] login | about

Link: http://codepad.org/H0CDN7ok    [ raw code | output | fork ]

PHP, pasted on Jul 4:
<?php
//this is the input
$array = array(
    'random' => 1,
    'pewpew' => 2,
    'temp' => 5,
    'xoxo' => 3,
    'qweqweqe' => 4,
);
//these are the fields that we need
$fields = array('random', 'xoxo', 'temp');

//create an array that has the fields as keys (and anything as values)
$tmp = array_combine($fields, range(1, count($fields)));
//merge the two arrays
$result = array_merge($tmp, $array);
//now we will have an array that has the keys from $fields in that order and the values from $array; 
//the only problem is that the extra items form $array are added at the end, so we need to remove them
$result = array_splice($result, 0, count($fields));//the final array needs to have the same number of elements as the $fields array

//output
print_r($result);


Output:
1
2
3
4
5
6
Array
(
    [random] => 1
    [xoxo] => 3
    [temp] => 5
)


Create a new paste based on this one


Comments: