Page 1 of 1

nested array? How to loop through?

Posted: Thu Aug 05, 2004 1:27 am
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

Posted: Thu Aug 05, 2004 1:30 am
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'].

Posted: Thu Aug 05, 2004 1:40 am
by feyd
Next time, use

Code: Select all

tags please, RobbertvanOs.

sorry for that

Posted: Thu Aug 05, 2004 2:03 am
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

Posted: Thu Aug 05, 2004 2:08 am
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]()

Posted: Thu Aug 05, 2004 2:16 am
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 ;)

Posted: Thu Aug 05, 2004 2:20 am
by feyd
0 and 1 would work correctly with your existing reading code..

Posted: Thu Aug 05, 2004 2:26 am
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

yeah!!

Posted: Thu Aug 05, 2004 12:20 pm
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

Posted: Thu Aug 05, 2004 2:07 pm
by d3ad1ysp0rk
See liquid. Sometimes they require everything done for them, other times you can just give them pointers.

Posted: Thu Aug 05, 2004 4:31 pm
by LiquidPro
Haha, yup.

I'm learning :)