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?

Answers


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

Need Your Help

UIBarButtonItem with stretched not tiled UIImage in iOS5

iphone ios uikit

I am using the appearance API to customise the look of my UIBarButtonItems.

Solr - Wild Card Search varies with Stemming Methods

solr wildcard stemming

I have 2 versions of solr working in my machine . say SolrVer1 and SolrVer2

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.