从3D关联数组中删除key =>值对 - PHP

时间:2012-09-06 10:23:46

标签: php multidimensional-array foreach associative-array

3D assoc。数组如下所示。

Array 
( 
    [COL] => Array 
                ( 
                    [0] => Array 
                        ( 
                            [emp_num] => 1000001 
                            [user_name] => Test User 
                            [amount] => 775.00 
                            [name] => COL 
                        ) 
                    [1] => Array 
                        ( 
                            [emp_num] => 26 
                            [user_name] => John Doe 
                            [amount] => 555.00 
                            [name] => COL 
                        ) 
                ) 
    [RA. 20%] => Array 
                ( 
                    [0] => Array 
                        ( 
                            [emp_num] => 1000001 
                            [user_name] => Test User 
                            [amount] => 110.00 
                            [name] => RA. 20% 
                        ) 
                ) 
    [BS] => Array 
                ( 
                    [0] => Array 
                        ( 
                            [emp_num] => 1000001 
                            [user_name] => Test User 
                            [amount] => 444.00 
                            [name] => BS 
                        ) 
                ) 
)  

我想删除每个最内层数组的最后key=>value对。 (想要删除具有[name]键的键值对) 结果应该类似于下面的数组。

Array 
( 
    [COL] => Array 
                ( 
                    [0] => Array 
                        ( 
                            [emp_num] => 1000001 
                            [user_name] => Test User 
                            [amount] => 775.00
                        ) 
                    [1] => Array 
                        ( 
                            [emp_num] => 26 
                            [user_name] => John Doe 
                            [amount] => 555.00
                        ) 
                ) 
    [RA. 20%] => Array 
                ( 
                    [0] => Array 
                        ( 
                            [emp_num] => 1000001 
                            [user_name] => Test User 
                            [amount] => 110.00
                        ) 
                ) 
    [BS] => Array 
                ( 
                    [0] => Array 
                        ( 
                            [emp_num] => 1000001 
                            [user_name] => Test User 
                            [amount] => 444.00
                        ) 
                ) 
)   

我写了一个函数来做到这一点。

<!-- language: php -->    
function remove_name_from_psa($psa_array){

            foreach( $psa_array as $key=>$value ) {
                foreach( $value as $key2=>$value2 ){
                    foreach( $value2 as $key3=>$value3 ){                    
                         if( $key3 != 'name') {
                            $psa_name_removed[$key][$value[$key2][$value2[$key3]]] = $value3;
                        }
                    }
                }
            }
            return $psa_name_removed;
        }  

返回的数组就是这个,这显然不是我需要的。

Array ( [COST OF LIVING] => Array 
                   ( [] => 555.00 ) 
        [RENT ALLOW. 20%] => Array 
                   ( [] => 110.00 ) 
        [BASIC SALARY] => Array 
                   ( [] => 444.00 ) 
      )  

还有很多未定义的偏移和未定义的索引通知 $psa_name_removed[$key][$value[$key2][$value2[$key3]]] = $value3; //这就是我犯错误的路线吗?或整个方法是一个错误? :-P
我怎样才能让它发挥作用?有人可以帮忙吗? 谢谢!

5 个答案:

答案 0 :(得分:2)

function remove_name_from_psa($psa_array){

    foreach( $psa_array as $key => $value ) {
        foreach( $value as $key2 => $value2 ){
            unset( $psa_array[$key][$key2]['name'] );
        }
    }
    return $psa_array;

} 

答案 1 :(得分:1)

Wee,功能性解决方案!

$array = array_map(function ($i) {
    return array_map(function ($j) {
        return array_diff_key($j, array_flip(array('name')));
    }, $i);
}, $array);

更传统的解决方案:

foreach ($array as &$i) {
    foreach ($i as &$j) {
        unset($j['name']);
    }
}

请注意&中的as &$i。使用此参考修改项目。

答案 2 :(得分:0)

foreach($array as &$foo){
    foreach($foo as &$bar){
        unset($bar['name']);
    }
}

答案 3 :(得分:0)

要真正取消设置3D数组中的最后一个元素,您可以这样做:

$data = array(
    array(
        array(1, 2, 3),
    ),
);

foreach ($data as $i1 => $j1) {
    foreach ($j1 as $i2 => $j2) {
        end($j2);
        unset($data[$i1][$i2][key($j2)]);
    }
}
var_dump($data);

请在此处查看:

http://codepad.viper-7.com/CbgnVf

答案 4 :(得分:0)

function remove_name_from_psa( $psa_array ){
    foreach( $psa_array as $key => $value ) {
        foreach( $value as $key2 => $value2 ) {
            array_pop( $psa_array[$key][$key2] );
        }
    }
    return $psa_array;
}