简体   繁体   English

循环遍历数组php时如何获取每个数组的名称

[英]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 获取每个数组的内容,例如“page_title”。


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.如何获取包含其他数据的数组的“page-1-name”名称。

Use the foreach statement as shown below to display the name of the key.使用如下所示的 foreach 语句来显示键的名称。 You can then use $val[key_name] to loop through value of the keys in the inner array然后,您可以使用$val[key_name]循环遍历内部数组中键的值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM