[SOLVED] nested array? How to loop through?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RobbertvanOs
Forum Newbie
Posts: 9
Joined: Wed Jul 02, 2003 2:43 pm

nested array? How to loop through?

Post by RobbertvanOs »

Hi there, i seem to have a small problem in my code, i try to loop over an array and display the items in a table structure. Before i can start "designing" the table, i should display any data :?

The array is like this:

$listings = array (
"item1" => array ( "a"=>"tester1",
"b"=>"tester1_b",
"c"=>"#link1",
"d"=>"/img/1.jpg"),

"item2" => array ( "a"=>"tester2",
"b"=>"tester2_b",
"c"=>"#link2",
"d"=>"/img/2.jpg"),
);

And the code i wrote is like this:

echo "<table>";
for ($i=0; $i<count($listings); $i++)
{ echo "<tr><td>";
echo $listings[$i]['a']."<br>".$listings[$i]['b'];
echo "</td><td>";
echo $listings[$i]['c']."<br>".$listings[$i]['d'];
echo "</td></tr>";
}
echo "</table>";

I can't seem to manage to get the array's within the array ($listing). Could someone perhaps explain me what i am doing wrong please?

The thing a want to accomplish is a table with an image an explanation text on the right. I want to build the layout ones and just add an extra array, if nescesary.

Thx in advance for any response,

Kind regards Robbert
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Just adding php tags..

Code: Select all

$listings = array (
"item1" => array ( "a"=>"tester1",
"b"=>"tester1_b",
"c"=>"#link1",
"d"=>"/img/1.jpg"),

"item2" => array ( "a"=>"tester2",
"b"=>"tester2_b",
"c"=>"#link2",
"d"=>"/img/2.jpg"),
);
And the code i wrote is like this:

Code: Select all

echo "<table>";
for ($i=0; $i<count($listings); $i++)
{ echo "<tr><td>";
echo $listings[$i]['a']."<br>".$listings[$i]['b'];
echo "</td><td>";
echo $listings[$i]['c']."<br>".$listings[$i]['d'];
echo "</td></tr>";
}
echo "</table>";
Odd.. it seems that mapping the names and values when creating the array almost made it impossible to access them via a number? ie. $array[1]['a'] no longer works.. however $array['item1']['a'] does, and so does $array['item'.$i]['a'].
Last edited by d3ad1ysp0rk on Thu Aug 05, 2004 1:40 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Next time, use

Code: Select all

tags please, RobbertvanOs.
RobbertvanOs
Forum Newbie
Posts: 9
Joined: Wed Jul 02, 2003 2:43 pm

sorry for that

Post by RobbertvanOs »

ok sorry for that, i am kind of new with these things, but you are right, it looks much better and it is more clear now.

The response above isn't a solution is it? Can it be done anyway?

Robbert
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well.. your array has named indices not numeric indices.. so you may want to use [php_man]foreach[/php_man]() or if you insist on continuing to use numerics, [php_man]array_keys[/php_man]()
RobbertvanOs
Forum Newbie
Posts: 9
Joined: Wed Jul 02, 2003 2:43 pm

Post by RobbertvanOs »

I can change the array, should i change "item1" and "item2" into 1 and 2? I can do that if that makes it work ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

0 and 1 would work correctly with your existing reading code..
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

Here we go... this is doing what you want, I believe.

Code: Select all

$listings = array ( 
	"item1" => array ( "a"=>"tester1", 
					   "b"=>"tester1_b", 
					   "c"=>"#link1", 
					   "d"=>"/img/1.jpg"
	), 
	
	"item2" => array ( "a"=>"tester2", 
					   "b"=>"tester2_b", 
					   "c"=>"#link2", 
					   "d"=>"/img/2.jpg"
	)
);

echo "<table>\n";
foreach($listings as $value) {
	$array = $value;

	echo "<tr>\n";
	echo "   <td>\n";
	echo "   " . $array['a'] . "<br>\n";
	echo "   " . $array['b'] . "\n";	
	echo "   </td>\n";
	echo "   <td>\n";
	echo "   " . $array['c'] . "<br>\n";
	echo "   " . $array['d'] . "\n";
	echo "   </td>\n";
	echo "</tr>\n";
}
echo "</table>\n";
:) Let me know if it's right
RobbertvanOs
Forum Newbie
Posts: 9
Joined: Wed Jul 02, 2003 2:43 pm

yeah!!

Post by RobbertvanOs »

This is working great :D thx people for your quick responses and taking the time to help me out. I also understand more about using array's thx!

Kind regards Robbert
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

See liquid. Sometimes they require everything done for them, other times you can just give them pointers.
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

Haha, yup.

I'm learning :)
Post Reply