I have a array like below
$arr=array(
array(
'id'=> 342,
'name' =>'srikanth',
'age' => 32
),
array(
'id'=> 409,
'name' =>'Ashok',
'age' => 24
),
array(
'id'=> 314,
'name' =>'Chakri',
'age' => 25
),
array(
'id'=> 208,
'name' =>'saikiran',
'age' => 27
)
);
I have to look for a specific id from the array for example id=409 which i am doing like below
$key=array_search("409",array_column($arr,"id"));
and copying the array to a temp variable like below and unsetting it:
$tmp=$arr[$key];
unset($arr[$key]);
Now what i want is to insert the temp array at my desired index in $arr.
I used below function to insert into my desired index but failed to get desired result.
function insertAt($array = [], $item = [], $position = 0) {
$previous_items = array_slice($array, 0, $position, true);
$next_items = array_slice($array, $position, NULL, true);
return $previous_items + $item + $next_items;
}
$arr=insertAt($arr,$tmp,0);
I want the temp array at 0 index (Not always at 0 index i know about array_unshift :) ) and my result array should look like this.
$arr=array(
array(
'id'=> 409,
'name' =>'Ashok',
'age' => 24
),
array(
'id'=> 342,
'name' =>'srikanth',
'age' => 32
),
array(
'id'=> 314,
'name' =>'Chakri',
'age' => 25
),
array(
'id'=> 208,
'name' =>'saikiran',
'age' => 27
)
);