Page 1 of 1

How to combine mulitple array sets into one?

Posted: Fri Feb 27, 2009 11:00 pm
by becky-atlanta
I created an order form. It looks something like this:

banana...........0.49/lb.....Qty ___
apple..............0.99/lb.....Qty ___
orange............0.50/ea....Qty ___

Once the customers finish putting in the quantity, they submit the form. The output form should have the line item selected and calculate the total dollar amount. If the customer is satisfy with the order, he/she then put in a few customer information and submit the final form.

These are some of the arrays I get from the initial form through var_dump:

["order"]=> array(9) {
[0]=> string(11) "Seder Plate"
[1]=> string(32) "Tri colored Gefilte Fish Terrine"
[2]=> string(13) "Chopped Liver"
[3]=> string(8) "Charoset"
[4]=> string(12) "Chicken Soup"
[5]=> string(21) "Butternut Squash Soup"
[6]=> string(37) "Mexican Red Pepper Soup (served cold)"
[7]=> string(37) "Chilled Roasted Tomato & Avocado Soup"
[8]=> string(11) "Matzo Balls" }

["price"]=> array(9) {
[0]=> string(5) "25.00"
[1]=> string(5) "22.50"
[2]=> string(5) "14.50"
[3]=> string(5) "14.50"
[4]=> string(4) "7.50"
[5]=> string(4) "8.50"
[6]=> string(4) "9.00"
[7]=> string(4) "9.00"
[8]=> string(4) "1.75" }

["qty"]=> array(9) {
[0]=> string(1) "1"
[1]=> string(1) "3"
[2]=> string(1) "1"
[3]=> string(1) "1"
[4]=> string(1) "2"
[5]=> string(0) ""
[6]=> string(0) ""
[7]=> string(0) ""
[8]=> string(2) "24" }

I know the array_combine function can combine 2 arrays but not 3. Is there a work around?

Thanks,
Becky

Re: How to combine mulitple array sets into one?

Posted: Fri Feb 27, 2009 11:47 pm
by Randwulf
You want to turn that into a multidimensional array like this:

$order['Cedar Plate']['25.00'] and that would equal 1

Is that correct?

I don't know how to do that with a function but you could process it with an array:

Code: Select all

 
$counter = 0;
foreach ($item : $order)
{
     $arr[$item][{$price[$counter]}] = $qty[$counter];
     $counter++;
}
 

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 8:11 am
by becky-atlanta
Thanks, Randwulf.

I tried your code but I am getting an syntax error on this line:

# foreach ($item : $order)

The : is unexpected. I am checking the syntax now...

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 9:17 am
by Randwulf
becky-atlanta wrote:Thanks, Randwulf.

I tried your code but I am getting an syntax error on this line:

# foreach ($item : $order)

The : is unexpected. I am checking the syntax now...
Oops, sorry, that was just me having a Java flashback.

It should be foreach ($order as $item)

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 10:49 am
by becky-atlanta
Randwulf,

I managed to get the syntax right. However, when I echo the variables to see what I am getting:

echo $item;

I could only get the name of the dishes. I could not get the price or the quantity.

I tried different combinations ($arr[$item], $arr[$price], $item[$price]...), but all end up getting a brunch of "Array" or Illegal error.

I am still trying...

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 11:27 am
by John Cartwright
From the sounds of it it you were close. You could do a for() loop to iterate each order and group them with the other array elements.

Something like

Code: Select all

 
$inventory = array(
    'order' => array(
        0 => 'chicken',
        1 => 'beef',
        2 => 'fish',
        3 => 'bread',
    ),
    'price' => array(
        0 => 25.00,
        1 => 30.00,
        2 => 22.50,
        3 => 10.00
    ),
    'qty' => array(
        0 => 5,
        1 => 10,
        2 => 15,
        3 => 50
    )
);
 
$newinventory = array();
 
$numproducts = count($inventory['order']);
for ($x=0; $x<$numproducts; $x++) {
    $newinventory[] = array(
        'order' => $inventory['order'][$x],
        'price' => $inventory['price'][$x],
        'qty' => $inventory['qty'][$x]
    );
}
 
echo '<pre>';
print_r($newinventory);

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 7:57 pm
by becky-atlanta
Hi, John,

Thanks again for giving me a hand. I try to applied your example.

However, your example has one multi-dimensional array to begin with. That means the data is already related to each other. The variables generated from my first form are 3 separate arrays as follows (through var-dump):

["order"]=> array(9) {
[0]=> string(11) "Seder Plate"
[1]=> string(32) "Tri colored Gefilte Fish Terrine"
[2]=> string(13) "Chopped Liver"
[3]=> string(8) "Charoset"
[4]=> string(12) "Chicken Soup"
[5]=> string(21) "Butternut Squash Soup"
[6]=> string(37) "Mexican Red Pepper Soup (served cold)"
[7]=> string(37) "Chilled Roasted Tomato & Avocado Soup"
[8]=> string(11) "Matzo Balls" }

["price"]=> array(9) {
[0]=> string(5) "25.00"
[1]=> string(5) "22.50"
[2]=> string(5) "14.50"
[3]=> string(5) "14.50"
[4]=> string(4) "7.50"
[5]=> string(4) "8.50"
[6]=> string(4) "9.00"
[7]=> string(4) "9.00"
[8]=> string(4) "1.75" }

["qty"]=> array(9) {
[0]=> string(1) "1"
[1]=> string(1) "3"
[2]=> string(1) "1"
[3]=> string(1) "1"
[4]=> string(1) "2"
[5]=> string(0) ""
[6]=> string(0) ""
[7]=> string(0) ""
[8]=> string(2) "24" }

Is it possible to apply your example in my situation? If not, I need to re-think the whole process.

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 8:03 pm
by John Cartwright
Which arrays do 'order', 'price', 'qty' keys belong to? Are those keys all in different arrays? You can always just combine those arrays into what I just described above by doing,

Code: Select all

$inventory = array(
   'order' => $somerandomarray['order'],
   'price' => $anotherarray['price'],
   'qty' => $anotherarrayagain['qty']
);

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 10:15 pm
by becky-atlanta
John Cartwright wrote:Which arrays do 'order', 'price', 'qty' keys belong to? Are those keys all in different arrays? You can always just combine those arrays into what I just described above by doing,
The keys are the default numeric keys generated by Array itself. The arrays came from $_POST variables. So how can I change the keys in an existing array?

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 10:19 pm
by John Cartwright
I was refering to the array that contained the keys 'order', 'price', 'qty', and not the keys belonging to each of those elements.

Basically, where are you getting this from?

["order"]=> array(9) {
[0]=> string(11) "Seder Plate"
...

["price"]=> array(9) {
[0]=> string(5) "25.00"
....

["qty"]=> array(9) {
[0]=> string(1) "1"
...

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 10:30 pm
by becky-atlanta
They are from the $_POST variables.

$qty = $_POST["qty"];
$order = $_POST["order"];
$price = $_POST["price"];

This is the var_dump($_POST):

array(4) {
["order"]=>
array(36) {
[0]=>
string(11) "Seder Plate"
[1]=>
string(32) "Tri colored Gefilte Fish Terrine"
[2]=>
string(13) "Chopped Liver"
[3]=>
string(8) "Charoset"
[4]=>
string(12) "Chicken Soup"
[5]=>
string(21) "Butternut Squash Soup"
[6]=>
string(37) "Mexican Red Pepper Soup (served cold)"
[7]=>
string(37) "Chilled Roasted Tomato & Avocado Soup"
[8]=>
string(11) "Matzo Balls"
[9]=>
string(12) "Cabbage Slaw"
[10]=>
string(18) "Grilled Vegetables"
[11]=>
string(14) "Broccoli Kugel"
[12]=>
string(12) "Potato Kugel"
[13]=>
string(12) "Squash Kugel"
[14]=>
string(18) "Sweet Potato Kugel"
[15]=>
string(21) "Farfel Kugel (savory)"
[16]=>
string(13) "Spinach Kugel"
[17]=>
string(11) "Apple Kugel"
[18]=>
string(15) "Apricot Chicken"
[19]=>
string(18) "Chicken Cacciatore"
[20]=>
string(12) "Coke Chicken"
[21]=>
string(13) "Fried Chicken"
[22]=>
string(9) "Schnitzel"
[23]=>
string(15) "Brisket of Beef"
[24]=>
string(39) "Italian Meatballs with Onions & Peppers"
[25]=>
string(24) "Sweet and Sour Meatballs"
[26]=>
string(15) "Stuffed Cabbage"
[27]=>
string(18) "Vegetarian Roulade"
[28]=>
string(38) "Sides of Smoked Salmon (weight varies)"
[29]=>
string(22) "Poached Salmon Fingers"
[30]=>
string(28) "Lemon Iced Sponge Bundt Cake"
[31]=>
string(17) "Apple Walnut Cake"
[32]=>
string(16) "Frosted Brownies"
[33]=>
string(17) "Black Forest Cake"
[34]=>
string(17) "Chocolate Roulade"
[35]=>
string(57) "½dz Chocolate Cream Puffs & ½dz Éclairs with Pastry Cream"
}
["price"]=>
array(36) {
[0]=>
string(5) "25.00"
[1]=>
string(5) "22.50"
[2]=>
string(5) "14.50"
[3]=>
string(5) "14.50"
[4]=>
string(4) "7.50"
[5]=>
string(4) "8.50"
[6]=>
string(4) "9.00"
[7]=>
string(4) "9.00"
[8]=>
string(4) "1.75"
[9]=>
string(4) "7.50"
[10]=>
string(4) "8.95"
[11]=>
string(5) "27.50"
[12]=>
string(5) "27.50"
[13]=>
string(5) "27.50"
[14]=>
string(5) "27.50"
[15]=>
string(5) "27.50"
[16]=>
string(5) "27.50"
[17]=>
string(5) "27.50"
[18]=>
string(4) "9.50"
[19]=>
string(4) "9.50"
[20]=>
string(4) "9.50"
[21]=>
string(5) "10.50"
[22]=>
string(5) "15.50"
[23]=>
string(5) "26.95"
[24]=>
string(5) "13.50"
[25]=>
string(5) "13.50"
[26]=>
string(5) "42.00"
[27]=>
string(5) "24.50"
[28]=>
string(5) "23.00"
[29]=>
string(5) "22.00"
[30]=>
string(5) "24.50"
[31]=>
string(5) "24.50"
[32]=>
string(5) "24.50"
[33]=>
string(5) "24.50"
[34]=>
string(5) "24.50"
[35]=>
string(5) "24.50"
}
["qty"]=>
array(36) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "1"
[3]=>
string(1) "1"
[4]=>
string(1) "1"
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(2) "36"
[9]=>
string(1) "1"
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(1) "1"
[13]=>
string(0) ""
[14]=>
string(1) "1"
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "1"
[18]=>
string(1) "1"
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(1) "2"
[24]=>
string(0) ""
[25]=>
string(1) "1"
[26]=>
string(0) ""
[27]=>
string(0) ""
[28]=>
string(1) "3"
[29]=>
string(1) "1"
[30]=>
string(1) "1"
[31]=>
string(0) ""
[32]=>
string(0) ""
[33]=>
string(0) ""
[34]=>
string(1) "1"
[35]=>
string(1) "1"
}
["Submit"]=>
string(9) "Order Now"
}

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 10:34 pm
by John Cartwright
In the script I posted earlier, you would just need to replace $inventory with $_POST (or do $inventory = $_POST;) then. However, I would add some error checking to make sure $_POST is populated before using it.

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 11:12 pm
by becky-atlanta
John,

I knew it got to be something simple, but I would never thought about that because I already set the variables at the beginning.

THANK YOU again!!! :D I was getting stressed. This is a very good lesson for me. I did not like programming in college. I am now hooked on php when I design websites in my spare time.

the humble amateur,
Becky

Re: How to combine mulitple array sets into one?

Posted: Sat Feb 28, 2009 11:20 pm
by John Cartwright
I'm glad you found this helpful!
I did not like programming in college. I am now hooked on php when I design websites in my spare time.
I did not like it at first either, but here I am :)