爆炸并在数组内插入一个元素

时间:2014-06-13 08:36:23

标签: php arrays

好吧,我有这个数组$city

[2] => Array
    (
        [0] => Array
            (
                [0] => fr
                [1] => paris
                [2] => a8
            )

        [1] => 0
        [2] => 116729
    )

[3] => Array
    (
        [0] => Array
            (
                [0] => fr
                [1] => marseille
                [2] => b8
            )

        [1] => 0
        [2] => 12898
    )

[4] => Array
    (
        [0] => Array
            (
                [0] => fr
                [1] => lyon
                [2] => b9
            )

        [1] => 0
        [2] => 8608
    )

依旧...... 我试图将其转换为:

[2] => Array
    (
        [0] => fr
        [1] => paris
        [2] => 0
        [3] => 116729
    )

[3] => Array
    (
        [0] => fr
        [1] => marseille
        [2] => 0
        [3] => 12898
    )

[4] => Array
    (
        [0] => fr
        [1] => lyon
        [2] => 0
        [3] => 8608
    )

以下是我的表现方式:

foreach ($city as $key => $value){
$city[$key][0] = $city[$key][0][0];
array_splice( $city[$key], 1, 0, $city[$key][0][1] );
 }

但这里的问题是结果数组:

[2] => Array
    (
        [0] => fr
        [1] => r
        [2] => 0
        [5] => 0
    )

[3] => Array
    (
        [0] => fr
        [1] => r
        [2] => 0
        [5] => 0
    )

[4] => Array
    (
        [0] => fr
        [1] => r
        [2] => 0
        [5] => 0
    )

我的代码中有什么错误,我可以解决吗?

4 个答案:

答案 0 :(得分:3)

试试这个

foreach($city as $key => $value) {
    $city[$key] = [
        $value[0][0], //fr
        $value[0][1], //lyon
        $value[1], //0
        $value[2], //8608
    ];
}

答案 1 :(得分:3)

完成后:

$city[$key][0] = $city[$key][0][0];

您无法引用原始$city[$key][0],因为您已将其取代。解决方案是首先将其保存在变量中:

foreach ($city as $key => &$value){
    $subarray = $value[0];
    $value[0] = $subarray[0];
    array_splice($value, 1, 0, $subarray[1] );
 }

我还使用了引用来避免在循环中重复$city[$key]

答案 2 :(得分:3)

您可以使用array_map代替foreach

$result = array_map(
    function ($v) { return array_merge(array_slice($v[0], 0, 2), array_slice($v, 1)); },
    $array
);

Demo

答案 3 :(得分:1)

在这里(如果您的数组在下面的示例中称为$new):     

    foreach ($item as $derp) {

        if (is_array($derp)) {
            foreach ($derp as $derrrrrr) {
                $d[$i][] = $derrrrrr;
            }
        } else {
            $d[$i][] = $derp;
        }
    }

    $i++;
}
?>

返回

Array
(
    [0] => Array
        (
            [0] => fr
            [1] => paris
            [2] => a8
            [3] => 0
            [4] => 11678
        )

    [1] => Array
        (
            [0] => fr
            [1] => paris
            [2] => a8
            [3] => 0
            [4] => 11678
        )

    [2] => Array
        (
            [0] => fr
            [1] => paris
            [2] => a8
            [3] => 0
            [4] => 11678
        )

)

修改

使用以下内容忽略第一个多维数组中的索引[2]。

$d = array();
$i = 0;
foreach ($new as $item) {

    foreach ($item as $derp) {

        if (is_array($derp)) {
            foreach ($derp as $key => $derrrrrr) {
                if ($key !== 2) {
                    $d[$i][] = $derrrrrr;
                }
            }
        } else {
            $d[$i][] = $derp;
        }
    }

    $i++;
}