0

I have the follow array, how do I write a loop to list the name and id?

Array
(
[data] => Array
    (
        [0] => Array
            (
                [name] => Test name
                [id] => 110
            )

        [1] => Array
            (
                [name] => Test name 2
                [id] => 111
            )

        [2] => Array
            (
                [name] => Test name 3
                [id] => 124
            )

        [3] => Array
            (
                [name] => Test name 4
                [id] => 105
            )

        [4] => Array
            (
                [name] => Test name 5
                [id] => 56
            )

    )

)

Thanks for any help :)

2 Answers 2

4

Like this:

foreach ($array['data'] as $key => $value) {
    echo $value['name'];
    echo $value['id'];
}
Sign up to request clarification or add additional context in comments.

Comments

2

Easy one:

foreach($array['data'] as $item){
    echo $item['id'].'. '.$item['name'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.