2017-03-07 64 views
0

以下的输出使$new_array包含多个阵列,其中iddatetype

$new_array = array(); 

foreach($things as $thing)(

    $new_array[] = array(
     'id' => $thing['id'], 
     'date' => '2017-01-01', 
     'type' => $thing['type'] 
    ); 

) 

如果我print_r($new_array)这让我所有的数组里面,但后来我想修改这个数组,并删除所有没有具体type其内部阵列。

为此,我假设我需要取消设置任何$new_array[]阵列,其中键值对type =>等于x

我该如何实现这个目标?我已经阅读了未设置的键值对,但这并不能帮助我处理多个数组。

+1

['$ new_array = array_filter($ new_array,函数($ N){返回$ N [ '型'] == 'X' ;});'](http://php.net/manual/en/function.array-filter.php)? – castis

+0

只需检查'$ thing ['type']'是否等于x,如果是,甚至不要将该子数组添加到数组中。如果你想过滤数组,你可能想使用'array_filter()'或者用一个简单的foreach循环和'unset()'来完成。 – Rizier123

+0

什么是$东西? – Datadimension

回答

0

也许你应该先检查加入阵列之前:

foreach($things as $thing){ 
    if(!empty($thing['type']) && $thing['type'] == 'my type'){ 
    $data[] = [ 
     'id' => $thing['id'], 
     'date' => '2017-01-01', 
     'type' => $thing['type'] 
    ]; 
    } 
} 

print_r($data);