Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>";   
      
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading