How to get the name of each array as you loop through an array of arrays php

Advertisements

I have an array of arrays as so:

$bookPages = array(
    "page-1-name" => array(
        "page_title" => "Search results",
        "page_name" => "search"
) ,
    "page-2-name" => array(
        "page_title" => "Front Cover | HCDP",
        "page_name" => "cover"
    )
  )

I am looping through to get the content each array like the "page_title" using a foreach.


foreach ( $bookPages as $bookPage ) {
    echo $bookPage["page_title]; 
    echo "page-1-name"; //This is the bit I need help with.
}

How do I get the name the "page-1-name" of the array that contains the other data.

>Solution :

Use the foreach statement as shown below to display the name of the key.
You can then use $val[key_name] to loop through value of the keys in the inner array

foreach ( $bookPages as $bookPage => $val ) {
    echo $bookPage . "<br>";   
      
    
}

Leave a ReplyCancel reply