0

I've used

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

to remove the first element of an array, but the selection form where the array is used no longer responds correctly to the radio button selected by the user. The original array looked like this:

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
    )

)

And the modified array looks like this:

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
    )

)

I suspect the problem is caused by the fact that in the modified array, [quote_i] now starts at 1, and not 0 as in the original. So i have [quote_i] as 1, 2 then 3 but it should be 0, 1, then 2.

I've tried using array_walk to correct this, but not been successful.

Any suggestions on a solution to this?

1

2 Answers 2

1

The trick basically would be to correct quote_i

$counter = 0;
foreach ($quotes_array[0]['methods'] as $key => $value)
{
    $quotes_array[0]['methods'][$key]['quote_i'] = $counter;
    $counter++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect solution. Doing it this way will correct ['quote_i'] however many there are in the array. Thanks. Another day where i've learned something new.
0

Using array_walk with example code that should match your use case

<?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))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.