Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组中循环数组,并在php中移除带值的键_Php - Fatal编程技术网

在数组中循环数组,并在php中移除带值的键

在数组中循环数组,并在php中移除带值的键,php,Php,我有一个以下格式的数组 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 ( 's

我有一个以下格式的数组

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')
)
我需要循环它并删除键“route\u risk\u score”,其值在所有数组中。如何在php中实现这一点。我是php新手。希望您能提供帮助(
$data = 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')
);
$count = count($data);
for ($i = 0; $i < count($data); $i++) {
     unset($data[$i]['route_risk_score']);
}
echo'<pre>';print_r($data);die;
output :
Array
(
[0] => Array
    (
        [safe_route] => yes
        [route_name] => route_1
    )

[1] => Array
    (
        [safe_route] => no
        [route_name] => route_2
    )

[2] => Array
    (
        [safe_route] => no
        [route_name] => route_3
    )

)
0=>数组( “安全路线”=>“是”, '路由\u名称'=>'路由\u 1', “路线风险评分”=>“2.5”), 1=>数组( “安全路线”=>“否”, '路由\u名称'=>'路由\u 2', “路线风险评分”=>“3.5”), 2=>数组( “安全路线”=>“否”, '路线名称'=>'路线3', “路线风险评分”=>“4.5”) ); $count=计数($data); 对于($i=0;$i阵列 ( [安全路线]=>是 [路由\u名称]=>路由\u 1 ) [1] =>阵列 ( [安全路线]=>否 [路由\u名称]=>路由\u 2 ) [2] =>阵列 ( [安全路线]=>否 [路由\u名称]=>路由\u 3 ) )
要删除原始数组中的元素,请对每个元素使用引用:


谢谢你,这对我很有帮助
// 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']);
}