cheetahes Posted August 10, 2011 Share Posted August 10, 2011 Okay, so. I'm kinda new to PHP, and thought I'd do a little array thing. It contains three (3) family names, and in the family, it contains some family members. I then want to output it like this: ---- Family Name 1 -------- Members of the family ---- Family Name 2 -------- Members of the family The full code that I use is this: [syntax=php]<html> <body> <?php $allfamilies = array ( "Jensen"=>array ( "Jarl", "Carlo", "Svend", "William", "Henning" ), "Knudsen"=>array ( "Karl", "John", "Peter" ), "Poulsen"=>array ( "Herman", "Egon", "Jens" ) ); sort($allfamilies); echo "PHP Array Test:"; foreach ($allfamilies as $family) { sort($family); echo "<br><br>---- " . $family; foreach ($family as $familymember) { echo "<br>-------- " . $familymember; } } ?> </body> </html> [/syntax] It outputs like this: PHP Array Test: ---- Array -------- Egon -------- Herman -------- Jens ---- Array -------- John -------- Karl -------- Peter ---- Array -------- Carlo -------- Henning -------- Jarl -------- Svend -------- William But it *should* (is supposed to) return like this: PHP Array Test: ---- Poulsen -------- Egon -------- Herman -------- Jens ---- Knudsen -------- John -------- Karl -------- Peter ---- Jensen -------- Carlo -------- Henning -------- Jarl -------- Svend -------- William So could anyone please help me make it output "---- Family Name" instead of "---- Array"? Thanks in advance! (: Link to comment https://forums.phpfreaks.com/topic/244412-looping-through-things-in-php-array/ Share on other sites More sharing options...
Alex Posted August 10, 2011 Share Posted August 10, 2011 What you want to do is change this line: foreach ($allfamilies as $family) $family is an array, but you want the key of that array, so change the foreach to this: foreach ($allfamilies as $familyname => $family) Then use $familyname in the echo. Link to comment https://forums.phpfreaks.com/topic/244412-looping-through-things-in-php-array/#findComment-1255350 Share on other sites More sharing options...
cheetahes Posted August 10, 2011 Author Share Posted August 10, 2011 Thanks for the quick reply, unfortunately it didn't do exactly as I wanted. I changed my array-looping to this: echo "PHP Array Test:"; foreach ($allfamilies as $familyname => $family) { sort($family); echo "<br><br>---- " . $familyname; foreach ($family as $familymember) { echo "<br>-------- " . $familymember; } } Instead of returning family name, it returns a number/integer (0/1/2) The full return of it, is this: PHP Array Test: ---- 0 -------- Egon -------- Herman -------- Jens ---- 1 -------- John -------- Karl -------- Peter ---- 2 -------- Carlo -------- Henning -------- Jarl -------- Svend -------- William Did I do something wrong? (: Link to comment https://forums.phpfreaks.com/topic/244412-looping-through-things-in-php-array/#findComment-1255355 Share on other sites More sharing options...
Alex Posted August 10, 2011 Share Posted August 10, 2011 Whoops. That's happening because you're using sort: sort($allfamilies); Instead use asort to maintain index association. Link to comment https://forums.phpfreaks.com/topic/244412-looping-through-things-in-php-array/#findComment-1255359 Share on other sites More sharing options...
cheetahes Posted August 10, 2011 Author Share Posted August 10, 2011 Thanks. It seems to work now, except that families are sorted a bit weird... Like reverse of alphabetical (: But I guess I'll be fine with that. Thanks so much! Link to comment https://forums.phpfreaks.com/topic/244412-looping-through-things-in-php-array/#findComment-1255360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.