Two Dimensional Array, Traversing It Using Nested While Loop No Foreach/For Loop

I’m having a hard time working with this array.

Okay so I have a multi dimensional array that I’m supposed to print out the values below but using the while loop only “while (list ($key, $value) = each($array))”.

Item one: price
Item two: price
Item three: price

Then, Total them = price + shipping + tax

[php]$products = array(
‘Candle Holder’ => array(
‘price’ => 11.95,
‘shipping’ => 0),
‘Coffee Table’ => array(
‘price’ => 99.50,
‘shipping’ => 0.10),
‘Floor Lamp’ => array(
‘price’ => 44.99,
‘shipping’ => 0.10)
);
// start table and print heading
while (list($key,$value) = each($products))
{
echo “$key: $value
”;
while (list($key,$value) = each($products))
{
echo “$key: $value
”;
}
// I have a function that calculates total here
}[/php]

By defining $key, $value again in your nested loop you are overwriting the variables in your first loop. So $key and $value become the results of the first index in the second loop.

On your first echo $value is an array, so you cannot echo an array.

See my comments in this code:

[php]
// start table and print heading
while (list($key,$value) = each($products))
{
// $value here is an array, you cannot echo it
//echo “$key: $value
”;
echo “$key:
”;

// use different variable names for second loop,
// changed to $pkey and $pvalue

// also, you are not re-looping $products, you are
// looping the nested array which is $value
while (list($pkey,$pvalue) = each($value))
{
	// modified to $pkey and $pvalue
	echo "$pkey: $pvalue<br>";
}
// I have a function that calculates total here

}
[/php]

I would also like to mention that using a while loop here is not recommended. This would be better suited using foreach.

Thank you for explaining that clearly!

Hello… now here’s my nested loop. It prints the following:

Item 1: $ the price: $ the shipping
Item 2: $ the price: $the shipping
Item 3: $ the price: $the shipping

But I’d like to take off the third value. Do you have any suggestions?

Here’s what I have so far:
[php]$products = array( array( product => “Candle Holder”,
“price” => 11.95,
“shipping” => 0
),
array( product => “Coffee Table”,
“price” => 99.50,
“shipping” => 0.10
),
array( product => “Floor Lamp”,
“price” => 44.99,
“shipping” => 0.10
)
);

while (list($key, $value) = each($products))
{
$p_tmp = 0; // price
$s_tmp = 0; // shipping

while(list($key2, $value2) = each($value))
{
if($key2 == “price”)
$p_tmp = $value2;

   if($key2 == "shipping")
      $s_tmp = $value2;      
   echo "$".$value2.": ";

}
echo “
”;
$grand_total += calculation($p_tmp, $s_tmp, $tax);
}
[/php]

I’m not sure if I understand what you are trying to do. It looks like you are unnecessarily looping the associative arrays. Also, your array gives undefined constant errors on product since it is not quoted properly.

Let me know if this does what you need:

[php]
$products = array(
array(
‘product’ => “Candle Holder”,
‘price’ => 11.95,
‘shipping’ => 0,
),
array(
‘product’ => “Coffee Table”,
‘price’ => 99.50,
‘shipping’ => 0.10,
),
array(
‘product’ => “Floor Lamp”,
‘price’ => 44.99,
‘shipping’ => 0.10,
),
);

$grand_total = 0;
while (list($key, $value) = each($products)) {
echo $value[‘product’] . ': ’ . $value[‘price’] . ‘
’, “\n”;
$grand_total += calculation($value[‘price’], $value[‘shipping’], $tax);
}
[/php]

I see… yeah this is exactly what I’m looking for. I thought you have to do another while loop if you have two dimensional arrays. Thank you again.

Well, you can loop them, if needed. If you only need to access the values there is no reason to loop each key/value pair. Also I’m just curious why you must use a while loop here? I find foreach() to be much better e.g.

[php]foreach($products as $key => $value) {[/php]

That’s what I hear, but while loop using list, each seems a bit challenging to use the foreach. So I wanted to concentrate with while loop it feels like it’s a bit cleaner I think.

It is a matter of preference. I’m not sure if there are performance benefits but I assume 1 function (foreach) is better than 3 (while, list, and each).

The while/list combo is good for query results if you want to fetch an array and have each column as a variable but for simple key/value pairs I don’t see the benefit.

Got it! Great thanks! :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service