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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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