0

我用过

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 来纠正这个问题,但没有成功。

关于解决此问题的任何建议?

4

2 回答 2

1

诀窍基本上是纠正quote_i

$counter = 0;
foreach ($quotes_array[0]['methods'] as $key => $value)
{
    $quotes_array[0]['methods'][$key]['quote_i'] = $counter;
    $counter++;
}
于 2013-05-03T10:07:41.010 回答
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))
}
于 2013-05-03T10:11:30.553 回答