Cleaning Up Array After unset()

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm working on an e-commerce site, and one of the things I need to do is split
an existing order into two orders.  The problem I'm having is not
creating the new order, but getting the remaining items from the original order
cleaned up in the array.  What I've tried to do so far is:

1) The data is stored in a serialized array in the order_data field in the
orders table.  When the order is selected, it is unserialized and called
$order_data.  Each item is a three item array containing price, quantity, and
sku (i.e. $order_data[$i]['price'], $order_data[$i]['quantity'], etc.).

2) On the order details page, the items in the order are listed using the price,
quantity, and sku fields.  There is a checkbox for each item that is
used to indicate the items to be split into the new order.  The name is split[]
(to create an array of just the selected items), and the value is the
sku.  The $order_data array is put into a session variable.

3) On the page that processes the split, $old_order = $_SESSION['order_data'].
So now I have two arrays: $old_data contains the entire original
order, and $split contains just the selected items.

4) I then go through and compare each item in $split[] against each item in
$old_order.  Once the current item in $split finds a match in $old_order,
it is put into $new_order, and I use unset() to remove that item from $old_order:

for ($i = 0; $i < count($split);$i++)
     foreach ($old_order as $key => $value)
        if (array_search($split[$i],$value))
            //add item to new order array
            $new_order[$new_counter]['attribute_sku'] = $value['attribute_sku'];
                $new_order[$new_counter]['price'] = $value['price'];
                $new_order[$new_counter]['quantity'] = $value['quantity'];

                //increment index counter
                $new_counter = $new_counter + 1;
                //increment order total
                $new_order_total = $new_order_total + ($value['price'] *

                //delete item from order_data array
                //increment old order total
                $modified_order_total = $modified_order_total + ($value['price'] *

That part works fine.  The problem I discovered is that unset leaves the other
indices in place, so when I loop through what remains of $old_order
using a for loop, I get errors on the indices that were unset.  Even if I try a
while loop to extract what remains in $old_order to a new array,
$modified_counter = 0;
while (list($key, $value) = each($old_order))
     while(list($sub_key,$sub_value) = each($value))
         $modified_order[$modified_counter][$sub_key] = $sub_value;
         $modified_counter = $modified_counter + 1;

I still can't get it to work.  So basically, I need to do one of two things:
1) Somehow compact $old_order and get rid of the empty indices, or
2) Take what remains in old_order, and put onnly the items that contain data
into a new array.
3) An unknown (to me) third option.

#2 seems to be the best option, but I can't seem to get that done.  If anyone
can shed some light on how to do what I need to do, I would greatly
appreciate it.



Re: Cleaning Up Array After unset()

Quoted text here. Click to load it

Once you finish building the $new_order array, try inserting:

$old_order = array_values($old_order);

That will reindex the array, eliminating the null elements. You should be able
to loop through it without errors.



Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg'); Web :
< | PHP scripts and thousands of webmaster resources!

Re: Cleaning Up Array After unset()

Senator Jay Billington Bulworth wrote:
Quoted text here. Click to load it
Works like a charm.  And to think that I could have saved myself hours with one
little line...



Site Timeline