修改数组内容

时间:2013-05-03 10:01:02

标签: php arrays

我用过

unset($quotes_array[0]['methods'][0]);
$quotes_array[0]['methods'] = array_values($quotes_array[0]['methods']);

删除数组的第一个元素,但使用数组的选择表单不再正确响应用户选择的单选按钮。 原始数组看起来像这样:

Array
(
[0] => Array
    (
        [id] => advshipper
        [methods] => Array
            (
                [0] => Array
                    (
                        [id] => 1-0-0
                        [title] => Trade Shipping
                        [cost] => 20
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 0
                    )

                [1] => Array
                    (
                        [id] => 2-0-0
                        [title] => 1-2 working days
                        [cost] => 3.2916666666667
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 1
                    )

                [2] => Array
                    (
                        [id] => 4-0-0
                        [title] => 2-3 working days
                        [cost] => 2.4916666666667
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 2
                    )

                [3] => Array
                    (
                        [id] => 8-0-0
                        [title] => Click & Collect
                        [cost] => 0
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 3
                    )

            )

        [module] => Shipping
        [tax] => 20
    )

)

修改后的数组如下所示:

Array
(
[0] => Array
    (
        [id] => advshipper
        [methods] => Array
            (
                [0] => Array
                    (
                        [id] => 2-0-0
                        [title] => 1-2 working days
                        [cost] => 3.2916666666667
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 1
                    )

                [1] => Array
                    (
                        [id] => 4-0-0
                        [title] => 2-3 working days
                        [cost] => 2.4916666666667
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 2
                    )

                [2] => Array
                    (
                        [id] => 8-0-0
                        [title] => Click & Collect
                        [cost] => 0
                        [icon] => 
                        [shipping_ts] => 
                        [quote_i] => 3
                    )

            )

        [module] => Shipping
        [tax] => 20
    )

)

我怀疑问题是由于在修改过的数组中,[quote_i]现在从1开始,而不是原始的0。所以我的[quote_i]为1,2,然后是3,但它应该是0,1,然后是2。

我尝试使用array_walk来纠正这个问题,但没有成功。

有关解决方案的任何建议吗?

2 个答案:

答案 0 :(得分:1)

诀窍主要是纠正quote_i

$counter = 0;
foreach ($quotes_array[0]['methods'] as $key => $value)
{
    $quotes_array[0]['methods'][$key]['quote_i'] = $counter;
    $counter++;
}

答案 1 :(得分:0)

将array_walk与示例代码一起使用,该代码应与您的用例匹配

<?php
foreach ($quotes_array[0]['methods'] as $a) {
    $a = array(
        array('quote_i' => 1),
        array('quote_i' => 2),
        array('quote_i' => 3)
        );

    array_walk($a, function(&$item, $key) {
        $item['quote_i'] = $item['quote_i'] - 1;
    });

    var_dump($a);

    // array([0] => array('quote_i' => 0), [1] => array('quote_i' => 1), [2] => array('quote_id' => 2))
}