查找特定的数组元素并删除容器数组

时间:2014-12-22 05:36:16

标签: php arrays

$assets = array(
'gp1' => array('XDEF390', 'XDEF302', '.RDS_EP01','XDEX11', '.RXL_EP01'),
'gp2' => array('XDEF390', 'XDEF300', 'XDEF302','XDEX11'),
'gp3' => array('XDEF395', 'XDEF300', 'XDEF302','XDEX11', '.RXL_EP01')
);

我需要删除每个内部数组,如果它包含以dot .开头的元素,这意味着资产不可重复使用!

我最终会:

$assets = array(
'gp2' => array('XDEF390', 'XDEF300', 'XDEF302','XDEX11'),
);

3 个答案:

答案 0 :(得分:1)

$assets = array(
'gp1' => array('XDEF390', 'XDEF302', '.RDS_EP01','XDEX11', '.RXL_EP01'),
'gp2' => array('XDEF390', 'XDEF300', 'XDEF302','XDEX11'),
'gp3' => array('XDEF395', 'XDEF300', 'XDEF302','XDEX11', '.RXL_EP01')
);

foreach($assets as $outer_key => &$inner_assets)
    foreach($inner_assets as $style)
        if($style{0} == '.') {
            unset($assets[$outer_key]);
            continue 2;
        }
var_dump($assets);

array(1) {
  ["gp2"]=>
  array(4) {
    [0]=>
    string(7) "XDEF390"
    [1]=>
    string(7) "XDEF300"
    [2]=>
    string(7) "XDEF302"
    [3]=>
    string(6) "XDEX11"
  }
}

答案 1 :(得分:1)

$assets = array_filter($assets, function (array $asset) {
    return !array_reduce($asset, function ($hasDot, $str) {
        return $hasDot || $str[0] === '.';
    });
});

答案 2 :(得分:1)

<?php 

$assets = array(
        'gp1' => array('XDEF390', 'XDEF302', '.RDS_EP01','XDEX11', '.RXL_EP01'),
        'gp2' => array('XDEF390', 'XDEF300', 'XDEF302','XDEX11'),
        'gp3' => array('XDEF395', 'XDEF300', 'XDEF302','XDEX11', '.RXL_EP01')
);

foreach( $assets as $key=>$val){
foreach($val as $key1=>$val1){
        if($val1[0] == '.') {
            unset($assets[$key]);
        }
}
}
print_r($assets);//Array ( [gp2] => Array ( [0] => XDEF390 [1] => XDEF300 [2] => XDEF302 [3] => XDEX11 ) ) 
?>