Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-Dimensional Arrays

Status
Not open for further replies.

job357

Technical User
Sep 16, 2002
106
US
can someone help me understand the following:

<?
$foo["travis"]["first"];
$foo["travis"]["second"];
$foo["wyndell"]["first"];
$foo["wyndell"]["second"];

foreach($outer = 0; $outer < count($foo); $outer++) {
foreach($outter as $inner => $value) {
echo "\$foo[$outer][$inner] =
$value<BR>";
}
}

?>

Question:
how can $outter be assigned again? I mean what will the values of $inner and $value be?

I have problems with nested arrays, and want you to help since you do a great job at this!
 
Outer is not being Set again, it is being used to get the value for the position in the array.

Basically your array is incomplete:

is should be $foo["travis"]["first"]="some value here";

The foreach loops through the array to get the value in every position and outputs its contents.


If you have ever playted battel ship, you know that a battelship has coordantes tha can be hit. same with an array.

It has coordantes and a alue in each particular coordenate.
such as:
$foo["travis"]["first"]="10"

that means that the valeu of the aray in the coordinates [red]travis[/red],[blue]first[/blue] has a value of 10.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Let me correct on that:

The foreach loops are wrong.
Foreach loops take first an array and the a variable in which to output the value of that array.

A correctuse for each is as follows:
Code:
foreach($foo as $bar){
foreach($bar as $value){
echo $value;
}
}
This will echo out every value in the array.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Here is the correction to my question:

<?php

$foo["travis"]["first"] = 90;
$foo["travis"]["second"] = 91 ;
$foo["wyndell"]["first"] = 92;
$foo["wyndell"]["second"] = 93;

foreach($foo as $outer_key => $single) {
foreach($single as $inner_key => $value) {
echo "\$foo[$outer][$inner] =
$value<BR>";
}
}

?>

The question I have is how is "$single" being used twice here, since is the value of $foo?
 
It's not exactly being used twice. In the exterior (first) foreach(), $single is just being instantiated. The variable $single is is begin used in the interior (second) foreach.

Your last-posted code is functionally equivalent to:

Code:
<?php

$foo["travis"]["first"] = 90;
$foo["travis"]["second"] = 91 ;
$foo["wyndell"]["first"] = 92;
$foo["wyndell"]["second"] = 93;

$single = $foo['travis'];
foreach($single as $inner_key => $value)
{
    echo "\$foo[travis][$inner] = $value<BR>";
}


$single = $foo['wyndell'];
foreach($single as $inner_key => $value)
{
    echo "\$foo[wyndell][$inner] = $value<BR>";
}

?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top