Steve Price
Steve Price

Reputation: 600

Modify array contents

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?

Upvotes: 0

Views: 91

Answers (2)

fullybaked
fullybaked

Reputation: 4127

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

Upvotes: 0

nvanesch
nvanesch

Reputation: 2600

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++;
}

Upvotes: 1

Related Questions