Reputation:
What would be the best method of moving any element of an associative array to the beginning of the array?
For example, say I have the following array:
$myArray = array(
'two' => 'Blah Blah Blah 2',
'three' => 'Blah Blah Blah 3',
'one' => 'Blah Blah Blah 1',
'four' => 'Blah Blah Blah 4',
'five' => 'Blah Blah Blah 5',
);
What i want to do is move the 'one' element to the beginning and end up with the following array:
$myArray = array(
'one' => 'Blah Blah Blah 1',
'two' => 'Blah Blah Blah 2',
'three' => 'Blah Blah Blah 3',
'four' => 'Blah Blah Blah 4',
'five' => 'Blah Blah Blah 5',
);
Upvotes: 108
Views: 51279
Reputation: 10114
You can use the array union operator (+
) to join the original array to a new associative array using the known key (one
).
$myArray = array('one' => $myArray['one']) + $myArray;
// or ['one' => $myArray['one']] + $myArray;
Array keys are unique, so it would be impossible for it to exist in two locations.
See further at the doc on Array Operators:
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
Upvotes: 210
Reputation: 2991
if you have 2 arrays, 1st has elements to move to the top of 2nd array of elements, you can use
$result = \array_replace($ArrayToMoveToTop, $myArray);
Here is a code sample:
//source array
$myArray = [
'two' => 'Blah Blah Blah 2',
'three' => 'Blah Blah Blah 3',
'one' => 'Blah Blah Blah 1',
'four' => 'Blah Blah Blah 4',
'five' => 'Blah Blah Blah 5',
];
// set necessary order
$orderArray = [
'one' => '',
'two' => '',
];
//apply it
$result = \array_replace($orderArray, $myArray);
\print_r($result);
Upvotes: 1
Reputation: 22941
Here's another simple one-liner that gets this done using array_splice()
:
$myArray = array_splice($myArray,array_search('one',array_keys($myArray)),1) + $myArray;
Upvotes: 2
Reputation: 677
A bit late, but in case anyone needs it, I created this little snippet.
function arr_push_pos($key, $value, $pos, $arr)
{
$new_arr = array();
$i = 1;
foreach ($arr as $arr_key => $arr_value)
{
if($i == $pos)
$new_arr[$key] = $value;
$new_arr[$arr_key] = $arr_value;
++$i;
}
return $new_arr;
}
Just adjust it to suit your needs, or use it and unset the index to move. Works with associative arrays too.
Upvotes: 2
Reputation: 34006
If you have numerical array keys and want to reindex array keys, it would be better to put it into array_merge
like this:
$myArray = array_merge(array($key => $value) + $myArray );
Upvotes: 7
Reputation: 91942
There's a function in the comments of the PHP manual for array_unshift which can be used to add an element, with key, to the beginning of an array:
function array_unshift_assoc(&$arr, $key, $val)
{
$arr = array_reverse($arr, true);
$arr[$key] = $val;
return array_reverse($arr, true);
}
Unset the element and reinsert it again with the above function:
$tmp = $myArray['one'];
unset($myArray['one']);
$myArray = array_unshift_assoc($myArray, 'one', $tmp);
A more general approach may be to use uksort to sort your array by keys and provide a sorting function of your own.
Upvotes: -2