473,503 Members | 12,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cleaning Up Array After unset()

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'] * $value['quantity']);

//delete item from order_data array
unset($old_order[$key]);
}
else
{
//increment old order total
$modified_order_total = $modified_order_total + ($value['price'] * $value['quantity']);
}
}
}

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.

Thanks.

Steve
Jul 17 '05 #1
2 3111
Steve <ra***********@hotmail.com> wrote in
news:ta********************@comcast.com:
[snip]
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.


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.

hth
--

Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg'); Web : shaunc.com
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
Jul 17 '05 #2
Senator Jay Billington Bulworth wrote:
Steve <ra***********@hotmail.com> wrote in
news:ta********************@comcast.com:

[snip]
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.

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.

hth

Works like a charm. And to think that I could have saved myself hours with one little line...

Thanks.

Steve
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2401
by: Tek9_AK | last post by:
I need to find a way to transfer all the values of an array inside a function out of the fuction into another array. IE function splice($filename){ if(file_exists($filename)){...
6
9076
by: SamMan | last post by:
Is there an easy way to re-initialize an array, other than looping through it's length and setting the values to null, or ""? Would unset() be a wise choice? Thanks again -- SamMan
0
1307
by: WebRod | last post by:
Hi, see this code, i only set an array of array, then i copy this array in another one, do an unset and display both: $a=array("row1.1"=>"row1.1.1" , "row1.2"=>"row1.2.1"); $a =...
8
3463
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
2
1991
by: Kaptain524 | last post by:
Hello, I am using PHP 5.0.4 with Apache 2, on WinXP Pro. This behavior appears to be fundamental however, and should not be affected by platform. It would seem that there is some kind of bug...
21
22888
by: vito | last post by:
how to achieve that? it seems php doesn't support it well for a C programmer? i hope to use something like: a; a; a;
1
4488
by: Nico | last post by:
Hi all, I've created the following code: <?php session_start(); ?> <FORM METHOD="POST" ACTION="prova.php"> Add <b>Combination</b><br><br>
2
2913
by: Snaggy | last post by:
I have a multidimensional array with a few entries I need to delete. Key and values are not unique in different sub arrays. As far as I know the filter function is not recursive and only deletes...
5
2296
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.