如何从PHP中的关联数组中删除二级索引

时间:2014-10-27 08:21:53

标签: php arrays associative-array

的print_r($ UNIQUE_ID);

Array
(
    [0] => Array
        (
            [LeadEmailDetail] => Array
                (
                    [id] => 97
                    [email] => vikastyagismartbuzz8@gmail.com
                )
        )

    [1] => Array
        (
            [LeadEmailDetail] => Array
                (
                    [id] => 93
                    [email] => vikastyagi87@gmail.com
                )
        )

    [2] => Array
        (
            [LeadEmailDetail] => Array
                (
                    [id] => 94
                    [email] => vikastyagismartbuzz9@gmail.com
                )
        )

)

但我想要那样

Array
(
    [0] => Array
        (
            [id] => 97
            [email] => vikastyagismartbuzz8@gmail.com
        )

    [1] => Array
        (
            [id] => 93
            [email] => vikastyagi87@gmail.com
        )

    [2] => Array
        (
            [id] => 94
            [email] => vikastyagismartbuzz9@gmail.com
        )

)

5 个答案:

答案 0 :(得分:1)

试试这个

foreach($unique_id as $k => $v)
{
    $array[] = array_shift($v);
}

答案 1 :(得分:1)

简单循环可以帮到你:

foreach ($array as &$v) {
    $v = current($v);
}
unset($v);

demo

<子> P.S。不像其他示例那样创建另一个数组(内存泄漏)。

答案 2 :(得分:1)

试试这个:

array_map('array_shift', $unique_id);

答案 3 :(得分:1)

print_r( array_column( $unique_id, 'LeadEmailDetail' ) );

需要PHP&gt; = 5.5.0

答案 4 :(得分:0)

谁构建了 $ unique_id 数组?我建议以你喜欢的形式构建它

解决您的问题......开箱即用又快又脏

<?php
    $new = array();
    foreach($unique_id as $arr) {
       foreach ($arr['LeadEmailDetail'] as $a) {
         $new[] = $a;
       }
    }
    print_r($new);