Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop array inside an array and remove key with its value in php

Tags:

php

I have an array in the below format.

array (
0 => 
array (
'safe_route' => 'yes',
'route_name' => 'route_1',
'route_risk_score' => '2.5'),
 1 =>
array (
'safe_route' => 'no',
'route_name' => 'route_2',
'route_risk_score' => '3.5'),
 2 =>
array (
'safe_route' => 'no',
'route_name' => 'route_3',
'route_risk_score' => '4.5')
)

i need to loop it and remove the key 'route_risk_score' with its value inside all arrays. How this can be done in php. Am new to php.Help would be appreciated

like image 713
Harnish Kumar Avatar asked Dec 07 '22 12:12

Harnish Kumar


1 Answers

To delete element in original array, use a reference to each element:

// see this & in front of `$item`? 
// It means that `$item` is a reference to element in original array 
// and unset will remove key in element of original array, not in copy
foreach($array as &$item) {          
    unset($item['route_risk_score']);
}
like image 106
u_mulder Avatar answered Apr 27 '23 05:04

u_mulder