0

I have a simple multidimensional array with two other arrays in it.

<?php
            $data = array(
              'first_array' => array(
                'name' => 'Test1',
                'description' => '...',
              ),
              'second_array' => array(
                'title' => 'Test2',
                'description' => '...',
              )
            );
        ?>

And I have a simple foreach array like this:

 function show($data, $id){

                 foreach ($data as $course) {

                 }

            }

How can I display (and get) the name of the array in every iteration (I mean if it is 'first_array' or 'second_array', not the name fields in the arrays).

1
  • Besides the answers below, take a look at array_keys($arr) Commented Mar 16, 2016 at 8:07

2 Answers 2

3

Use key=>val syntax

foreach ($data as $key=>$course) {
    echo $key;
}
Sign up to request clarification or add additional context in comments.

Comments

2

use this syntax for foreach :

foreach ($data as $name => $course) {
    //do sth
}

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.