将数组中的爆炸字符串推送到多维数组中的另一个元素

时间:2018-04-18 15:59:41

标签: php arrays

这是我的数组的样子

Array (
    [confirmshipment] => Array (

        [columndata] => Array (

            [0] => Array (

                    [0] => 
                    [1] => SG - NOUR RIBTS - CATHUPP
                    [2] => Nour Ribt Spaxacol Blenk
                    [3] => 800
                    [4] => 
                    [5] => 07-72-7016
                    [6] => Ecoaxan
                    [7] => NOUR RIBTS 07-72-7016
                    [8] => 
                         )

            [1] => Array (

                    [0] => 
                    [1] => SG - NOUR RIBTS - CATHUPP
                    [2] => Nour Ribt Spaxacol Loaxal
                    [3] => 800
                    [4] => 
                    [5] => 07-72-7016
                    [6] => Ecoaxan
                    [7] => NOUR RIBTS 07-72-7016
                    [8] => 

                         )

                              )

    [myCheckboxes] => 1,1
    [panelnumberx] => 0
                                )
       )                                

这是通过$_POST ajax回复/ $confirmshipment = $_POST['confirmshipment']; / $.post( ajaxurl, { confirmshipment: {columndata,myCheckboxes,panelnumberx} }

来实现的

现在我要做的是将myCheckboxescolumndata分组(这些将始终是相同的数组)

我可以轻松地将myCheckbox变为数组

 $checkboxes = explode(',', $params['confirmshipment']['myCheckboxes']);

制作新的$checkboxes数组

Array
(
    [0] => 1
    [1] => 1
)

我也尝试在foreach循环中使用&$引用来修改原始数组..

$c = 0;
foreach ($checkboxes as $checked) {
foreach ($params as $key['columndata'] => &$value) {
    $value['columndata']['ooscheck'][$c] = $checked;
    $c++;
}
}

让它看起来像这样

Array (
    [confirmshipment] => Array (

        [columndata] => Array (

            [0] => Array (

                    [0] => 
                    [1] => SG - NOUR RIBTS - CATHUPP
                    [2] => Nour Ribt Spaxacol Blenk
                    [3] => 800
                    [4] => 
                    [5] => 07-72-7016
                    [6] => Ecoaxan
                    [7] => NOUR RIBTS 07-72-7016
                    [8] => 
                         )

            [1] => Array (

                    [0] => 
                    [1] => SG - NOUR RIBTS - CATHUPP
                    [2] => Nour Ribt Spaxacol Loaxal
                    [3] => 800
                    [4] => 
                    [5] => 07-72-7016
                    [6] => Ecoaxan
                    [7] => NOUR RIBTS 07-72-7016
                    [8] => 

                         )

            [ooscheck] => Array
                        (
                            [0] => 1
                            [1] => 1
                        )

                              )

    [myCheckboxes] => 1,1
    [panelnumberx] => 0
                                )
       )

还尝试array_combine ... $c = array_combine($params['confirmshipment']['columndata'], $checkboxes);,但收到Warning: array_combine(): Both parameters should have an equal number of elements错误

我想要实现的是我的columndata数组输出看起来像这样

[columndata] => Array (

    [0] => Array (

            [0] => 
            [1] => SG - NOUR RIBTS - CATHUPP
            [2] => Nour Ribt Spaxacol Blenk
            [3] => 800
            [4] => 
            [5] => 07-72-7016
            [6] => Ecoaxan
            [7] => NOUR RIBTS 07-72-7016
            [8] => 
            [9] => 1
                 )

    [1] => Array (

            [0] => 
            [1] => SG - NOUR RIBTS - CATHUPP
            [2] => Nour Ribt Spaxacol Loaxal
            [3] => 800
            [4] => 
            [5] => 07-72-7016
            [6] => Ecoaxan
            [7] => NOUR RIBTS 07-72-7016
            [8] => 
            [9] => 1
                 )

                      )

注意新数组元素(从复选框数组中推送),[9] => 1

或者从技术上讲,由于[8] =>始终为空,因此也可以将其附加到此数字。

这是以这种方式遍历和修改数组的最佳选择吗?

2 个答案:

答案 0 :(得分:2)

虽然我不完全理解您的代码,但我认为这应该有效:

// Loop the exploded checkboxes
foreach( $checkboxes as $k=>$v )
{
    // Since the index of the columndata matches the index of the $checkboxes
    // we can safely just add the checkbox value to that columndata position
    $params['confirmshipment']['columndata'][$k][] = $v;
}

答案 1 :(得分:1)

你真的在数组中设置了ooscheck键,这与你试图完成的内容相反。

您可以使用array_push(&$array, $value)

将其附加到数组的末尾

docs:http://php.net/manual/en/function.array-push.php

使用您自己的代码,这是一些未经测试的伪代码,应该是正确的或非常接近的。

$c = 0;
foreach ($checkboxes as $checked) {
    foreach ($params as $key['columndata'] => &$value) {
        array_push($value['columndata'][$c], $checked);
        $c++;
    }
}

这会将$checked值推送到$value['columndata'][$c]的末尾,而不是ooscheck数组中的新$value['columndata']键。