Jump to content

Please help with 2D array


2DQ

Recommended Posts

$products_size_array = array(array('size' => '', 'sizecount' => ''));
$products_size_query = tep_db_query("select products_id, products_size, products_sizecount from " .  TABLE_PRODUCT_SIZE . " where products_id = '" . (int)$HTTP_GET_VARS['pID'] . "' order by products_size");

while ($products_size_detail = tep_db_fetch_array($products_size_query)) {
$products_size_array[] = array('size' => $products_size_detail['products_size'],
    'sizecount' => $products_size_detail['products_sizecount']
);
}

[b]so I have the array above called $products_size_array, each row has 2 elements: size and sizecount
How do I output the array like the following[/b]: 

size[0]/sizecount[0], size[1]/sizecount[1]

I have size 5 quantity is 3
I have size 6 quantity is 2

ex:  5/3, 6/2, .....

I tried this

<?
$count = count($products_size_array);
for ($i = 0; $i < $count; $i++) {
  echo $products_size_array['$i'] . ",";
}
?>

[b]All it outputs is[/b]:  ,,,
[b]With this code[/b]: <?php print_r($products_size_array); ?>
[b]I can see that the array does have values in it.  It outputs the below[/b]:
Array ( [0] => Array ( [size] => [sizecount] => ) [1] => Array ( [size] => 5 [sizecount] => 3 ) [2] => Array ( [size] => 6 [sizecount] => 2 ) )

[b]what am I doing wrong?[/b]


Link to comment
https://forums.phpfreaks.com/topic/26657-please-help-with-2d-array/
Share on other sites

Your array has keys withthe names 'size' and 'sizecount' and in your loop you are trying to call them with 0 & 1.

Try this:
[code]
foreach ($products_size_array as $value) {
    echo "I have size $value[size] quantity is $value[sizecount]<br>";
}
[/code]
One problem is that the very first line from your first post is creating some un-needed indexes in the array. Change that to:

$products_size_array = array();

It was kind of hard to spot that the way that you had printed out the array data. Once you make that change, if you are still having the problem, try outputting the array like this so it is more readable:

echo "<pre>";
print_r($products_size_array);
echo "<pre>";
try
[code]
<?php
$product_size_array = array(
    array ('size' => 5, 'sizecount' => 2),
    array ('size' => 6, 'sizecount' => 3) 
);

foreach ($product_size_array as $data) {
    echo join ('/', $data) . '<br>';

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.