Have an array like this
Array ( [] => [3837920201e05ba7c2fbffd3f1255129] => 'bg img a href Main | Delete etc' [94ae40ff9b6df5bb123fb12211f48b11] => 'bg img a href Main | Delete etc' [3974b3863e7ca7b7ea2026e44bbacfd2] => 'bg img a href Main | Delete etc' )
Want to move the 3974b3863e7ca7b7ea2026e44bbacfd2 key 3974b3863e7ca7b7ea2026e44bbacfd2 top so that the array looks like
Array ( [] => [3974b3863e7ca7b7ea2026e44bbacfd2] => 'bg img a href Main | Delete etc' [3837920201e05ba7c2fbffd3f1255129] => 'bg img a href Main | Delete etc' [94ae40ff9b6df5bb123fb12211f48b11] => 'bg img a href Main | Delete etc' )
First, extract the item that I want to be at the top
$top_image = array_slice( $array, 2, 1 );
3974b3863e7ca7b7ea2026e44bbacfd2 - the third element (as a key) in the array (0,1,2)
Next, you need to create an array of $other_images . I decided to remove the initial third element, and then combine both arrays.
Trying to remove the third item. Read [array_splice][1] understand that the first number (offset) is where I want to start deleting, and the second (length) is how many elements I need to delete. So I tried
$top_image = array_splice( $array, 2, 1 );
But the result will be the same as with array_slice.
Then tried
foreach( $arr as $k => $val ){ if( $k != 2 ){ $other_images[] = $val; } }
Expect to see the 2 remaining items. But see All 3.
What's wrong? How to remove a specific element from an array?
Regarding foreach $k cannot be equal to 2, because $k is a long string ... I tried for , but also does not work ...