Jump to content

Looping through things in PHP-array


cheetahes

Recommended Posts

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

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.

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? (:

Archived

This topic is now archived and is closed to further replies.

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