Jump to content

Multi dimensional Array, retreiving index value, not data type


PC Nerd

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.