PC Nerd Posted February 11, 2007 Share Posted February 11, 2007 hi guys. when i use the following code, i echo out $index1. but i get "Array" how can i use this foreach to get the string value of the index??? foreach($ARRAY_EG as $index1) { echo $index1; foreach ($index1 as $index2 => $Value) { echo "<tr>\n"; echo "<td width = '25%'>".$index1."</td>\n"; echo "<td width = '25%'>".$ARRAY_EG['$index1']['Name']."</td>\n"; echo "<td width = '25%'>".$ARRAY_EG['$index1']['Cost']."</td>\n"; echo "<td width = '25%'>".$ARRAY_EG['$index1']['Stock']."</td>\n"; echo "</td>\n"; #echo $ARRAY_EG['$index1']['Name']." => ".$Value."<br>\n"; } } thankx Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 11, 2007 Share Posted February 11, 2007 The same way you did in the second foreach... foreach($ARRAY_EG as $k=>$index1) { echo $key Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted February 12, 2007 Author Share Posted February 12, 2007 ok that doesnt work. ill expand on my querstion my array looks like : $Array['Apple']['Name'] = "Fruit"; $Array['Apple']['Price'] = "$2.50"; $Array['Carrot']['Name'] = "Vegetable"; $Array['Carrot']['Price'] = "$1.70"; $Array['Lamb']['Name'] = "Meat"; $Array['Lamb']['Price'] = "$5.50"; now i want to display a table that looks like: Fruit -- Apple -- $2.50 Vegetables -- Carrot -- $1.70 Meat -- Lamb -- $5.50 now my code is: foreach($Array as $index1) { foreach ($index1 as $index2 => $Value) { echo "<tr>\n"; echo "<td width = '25%'>".$index1."</td>\n"; echo "<td width = '25%'>".$Array['$index1']['Name']."</td>\n"; echo "<td width = '25%'>".$Array['$index1']['Price']."</td>\n"; echo "<td width = '25%'>".$Array['$index1']['Type']."</td>\n"; echo "</td>\n"; #echo $Array['$index1']['Name']." => ".$Value."<br>\n"; i may have copied this incorrectly from my printed copy becauase im not at the computer witht e code, but i think its right. whats hayyening is my table just diaplays: Array Array Array Array for all the entries, so if my array was 20 long, then it would display 20 times. so thats haooening is instead if retreiving the name for the embedded array, its retreiving its data type, and therefore not succseefuly its value. could anyone help me on this thankx Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 12, 2007 Share Posted February 12, 2007 Sorry for sounding harsh but what a terrible method of indexing an array... This is far more logical.. $array['type'][0] = 'Fruit'; $array['name'][0] = 'Apple'; $array['price'][0] = '2.5'; $array['type'][1] = 'Vegetable'; $array['name'][1] = 'Carrot'; $array['price'][1] = '1.7'; $array['type'][1] = 'Meat'; $array['name'][1] = 'Lamb'; $array['price'][1] = '5.5'; then you could do all sorts.... <?php if ( count($array['type']) > 0) // is the array populated { ?> <table> <?php $fields = array_keys($array); // how many fields in there foreach ($fields as $fkey => $fval) { ?> <th><?php echo $fval]; ?></th> <?php } ?> </tr> <?php $types = sort(array_unique($array['type'])); // extract types - alphabetically. foreach($types as $key => $value) // loop through types. { $temp = array_keys($array['type'], $value); // get all items in type. foreach ($temp as $tkey => $tval) { ?> <tr> <?php foreach ($fields as $fkey => $fval) { ?> <td><?php echo $array[$fval][$tval]; ?></td> <?php } ?> </tr> <?php } } ?> </table> <?php That is fairly robust... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 12, 2007 Share Posted February 12, 2007 For your particular example, you don't need two foreach loops. You can get the display you want like this: <?php echo '<pre>'; foreach ($Array as $k=>$v) echo str_replace(' ',' ',sprintf("%-10s -- %-10s -- %s",$v['Name'],$k,$v['Price']))."\n"; echo '</pre>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 12, 2007 Share Posted February 12, 2007 Indeed - I was just writing it so that it would manage any 2d array. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.