1

3D 协会。数组如下所示。

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
我怎样才能让它工作?任何人都可以帮忙吗?
谢谢你!

4

5 回答 5

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;

} 
于 2012-09-06T10:34:44.380 回答
1

哎呀,功能性解决方案!

$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. 使用此参考来修改项目。

于 2012-09-06T10:29:26.590 回答
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

于 2012-09-06T10:34:34.247 回答
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;
}
于 2012-09-06T11:17:47.027 回答
0
foreach($array as &$foo){
    foreach($foo as &$bar){
        unset($bar['name']);
    }
}
于 2012-09-06T10:29:30.083 回答