Move Value in PHP Array to the Beginning of the Array

PHP: Move associative array element to beginning of array

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.

Move array element by associative key to the beginning of an array

This seems, funny, to me. But here ya go:

$test = array(
'bla' => 123,
'bla2' => 1234,
'bla3' => 12345
);

//store value of key we want to move
$tmp = $test['bla2'];

//now remove this from the original array
unset($test['bla2']);

//then create a new array with the requested index at the beginning
$new = array_merge(array('bla2' => $tmp), $test);

print_r($new);

Output looks like:

Array
(
[bla2] => 1234
[bla] => 123
[bla3] => 12345
)

You could turn this into a simple function that takes-in a key and an array, then outputs the newly sorted array.

UPDATE

I'm not sure why I didn't default to using uksort, but you can do this a bit cleaner:

$test = array(
'bla' => 123,
'bla2' => 1234,
'bla3' => 12345
);

//create a function to handle sorting by keys
function sortStuff($a, $b) {
if ($a === 'bla2') {
return -1;
}
return 1;
}

//sort by keys using user-defined function
uksort($test, 'sortStuff');

print_r($test);

This returns the same output as the code above.

Moving array element to beginning of array?

array_splice removes (and optionally replaces / inserts) values from an array returning an array with the removed items. In conjunction with the simple array_unshift function the job could be done.

$arr = [1,2,3,4,5];

function array_move_item_as_first(array $array, int $idx) : array
{
array_unshift( $array, array_splice($array, $idx, 1)[0] );
return $array;
}

print_r(array_move_item_as_first($arr, 2));

output:

Array
(
[0] => 3
[1] => 1
[2] => 2
[3] => 4
[4] => 5
)

Move array item with certain key to the first position in an array, PHP

No need to unset keys. To keep it short just do as follow

//appending $new in our array 
array_unshift($arr, $new);
//now make it unique.
$final = array_unique($arr);

Demo

Move an element of an array from start to end

Within your id_fine function reset the array values of your array using array_values

$array_disordinato[0] = array_values($array_disordinato[0]);
return $array_disordinato

How do I move an array element with a known key to the end of an array in PHP?

The only way I can think to do this is to remove it then add it:

$v = $my_array['monkey'];
unset($my_array['monkey']);
$my_array['monkey'] = $v;

Move an array element to a new index in PHP

As commented, 2x array_splice, there even is no need to renumber:

$array = [
0 => 'a',
1 => 'c',
2 => 'd',
3 => 'b',
4 => 'e',
];

function moveElement(&$array, $a, $b) {
$out = array_splice($array, $a, 1);
array_splice($array, $b, 0, $out);
}

moveElement($array, 3, 1);

redzarf comments: "To clarify $a is $fromIndex and $b is $toIndex"

Result:

[
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
];

How to insert an item at the beginning of an array in PHP?

Use array_unshift($array, $item);

$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);

will give you

Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)


Related Topics



Leave a reply



Submit