2DQ Posted November 9, 2006 Share Posted November 9, 2006 $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 sizecountHow do I output the array like the following[/b]: size[0]/sizecount[0], size[1]/sizecount[1]I have size 5 quantity is 3I have size 6 quantity is 2ex: 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 More sharing options...
Psycho Posted November 9, 2006 Share Posted November 9, 2006 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] Link to comment https://forums.phpfreaks.com/topic/26657-please-help-with-2d-array/#findComment-121965 Share on other sites More sharing options...
2DQ Posted November 9, 2006 Author Share Posted November 9, 2006 I did above and it outputs this5 / 36 / 26 / 26 / 26 / 2Which it shouldntthere are only 2 sizes in the tablesize 5 quantity 3size 6 quantity 2I dont know why its repeating 6/2 Link to comment https://forums.phpfreaks.com/topic/26657-please-help-with-2d-array/#findComment-121974 Share on other sites More sharing options...
Psycho Posted November 9, 2006 Share Posted November 9, 2006 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>"; Link to comment https://forums.phpfreaks.com/topic/26657-please-help-with-2d-array/#findComment-121988 Share on other sites More sharing options...
JasonLewis Posted November 9, 2006 Share Posted November 9, 2006 couldn't you use the array_unique() function to group similar arrays. Link to comment https://forums.phpfreaks.com/topic/26657-please-help-with-2d-array/#findComment-122010 Share on other sites More sharing options...
Barand Posted November 9, 2006 Share Posted November 9, 2006 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] Link to comment https://forums.phpfreaks.com/topic/26657-please-help-with-2d-array/#findComment-122282 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.