PHP通过键值访问数组项



我有一个php数组($menu)与子项目。我想循环内的项目具有相同的父ID,例如[post_parent] =>36

foreach($menu as $m) {
*where post_parent => 36 ???*
echo $m->title;
}

这是我的数组:

Array
(
[0] => WP_Post Object
(
[ID] => 110
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] => 
[post_title] => L’école
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => lecole
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 0
[guid] => http://localhost:8888/?p=110
[menu_order] => 1
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 110
[menu_item_parent] => 0
[object_id] => 36
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/lecole/
[title] => L’école
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[1] => WP_Post Object
(
[ID] => 114
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] =>  
[post_title] => 
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => 114
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 36
[guid] => http://localhost:8888/?p=114
[menu_order] => 2
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 114
[menu_item_parent] => 110
[object_id] => 45
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/lecole/a-propos/
[title] => À propos
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[2] => WP_Post Object
(
[ID] => 113
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] =>  
[post_title] => 
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => 113
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 36
[guid] => http://localhost:8888/?p=113
[menu_order] => 3
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 113
[menu_item_parent] => 110
[object_id] => 103
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/lecole/le-lieu/
[title] => Le lieu
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[3] => WP_Post Object
(
[ID] => 112
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] => 
[post_title] => L’équipe
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => lequipe
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 36
[guid] => http://localhost:8888/?p=112
[menu_order] => 4
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 112
[menu_item_parent] => 110
[object_id] => 106
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/lecole/lequipe/
[title] => L’équipe
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[4] => WP_Post Object
(
[ID] => 115
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] => 
[post_title] => Les alumni
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => les-alumni
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 0
[guid] => http://localhost:8888/?p=115
[menu_order] => 5
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 115
[menu_item_parent] => 110
[object_id] => 115
[object] => custom
[type] => custom
[type_label] => Lien personnalisé
[title] => Les alumni
[url] => http://localhost:8888/lecole/test/
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[5] => WP_Post Object
(
[ID] => 111
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] =>  
[post_title] => 
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => 111
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 36
[guid] => http://localhost:8888/?p=111
[menu_order] => 6
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 111
[menu_item_parent] => 110
[object_id] => 108
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/lecole/les-partenaires/
[title] => Les partenaires
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[6] => WP_Post Object
(
[ID] => 116
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] => 
[post_title] => Le fond de dotation
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => le-fond-de-dotation
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 0
[guid] => http://localhost:8888/?p=116
[menu_order] => 7
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 116
[menu_item_parent] => 110
[object_id] => 116
[object] => custom
[type] => custom
[type_label] => Lien personnalisé
[title] => Le fond de dotation
[url] => http://localhost:8888/lecole/test/
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[7] => WP_Post Object
(
[ID] => 117
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] =>  
[post_title] => 
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => 117
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 0
[guid] => http://localhost:8888/?p=117
[menu_order] => 8
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 117
[menu_item_parent] => 0
[object_id] => 43
[object] => page
[type] => post_type
[type_label] => Page
[url] => http://localhost:8888/les-formations/
[title] => Les formations
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
[8] => WP_Post Object
(
[ID] => 118
[post_author] => 1
[post_date] => 2021-01-26 18:29:03
[post_date_gmt] => 2021-01-26 17:29:03
[post_content] => 
[post_title] => C'est public
[post_excerpt] => 
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] => 
[post_name] => cest-public
[to_ping] => 
[pinged] => 
[post_modified] => 2021-01-26 18:29:03
[post_modified_gmt] => 2021-01-26 17:29:03
[post_content_filtered] => 
[post_parent] => 0
[guid] => http://localhost:8888/?p=118
[menu_order] => 9
[post_type] => nav_menu_item
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[db_id] => 118
[menu_item_parent] => 0
[object_id] => -20
[object] => evenements
[type] => post_type_archive
[title] => C'est public
[type_label] => Archive du type de publication
[url] => http://localhost:8888/evenements/
[target] => 
[attr_title] => 
[description] => 
[classes] => Array
(
[0] => 
)
[xfn] => 
)
)

我知道这很基本,但我找不到解决方案…由于

您应该使用foreach并查找post_parent = 36,如果条件如下

foreach($menu as $m) {
if (isset($m->post_parent) && $m->post_parent == 36) {
echo "post_parent  36";
}
}

您提供的示例代码可以按以下方式完成。它将简单地回显父项大于36的每个项的标题。

foreach($menu as $m) {
if ($m['post_parent'] >= 36) {
echo $m['title'];
}
}

我怀疑可能正在寻找分组菜单项(例如打印父项0的每个标题,然后是父项1,然后是36,等等)。如果您想将它们分组并一起打印,您可以尝试这样做:

(根据需要修改)

$groups = array(); 
// Group items by parent value
foreach($menu as $m) {
$parent = $m['parent']; 
// Add to group (if exists). Otherwise, we must create the group. 
if (!isset($groups[$parent)) {
$groups[$parent] = array($m); // Create new key in array
} 
else {
$groups[$parent][] = $m; // Append to array
}
}
// Sort them in ascending order (by parent/key) 
$groups = ksort($groups); 
// Print groups in order
// Outer loop: Iterate through groups (each element contains a list of every item with the same parent)
foreach ($groups as $group) {
// Inner loop: Loop through each item in this group:  
foreach ($group as $item) {
// Edit this as needed. 
echo "Parent: " . $item['parent'] . ". Title: " . $item['title'];
}
}

上面的代码首先按父菜单项对每个菜单项进行分组,并将它们一起打印出来。这是你所需要的吗?编辑:如果每个项存储为对象而不是数组,则替换以下内容:

  • 项目("母公司")与美元项→父母
  • 将$m['parent']替换为$m->parent
  • 将$item['title']替换为$item->title

相关内容

  • 没有找到相关文章

最新更新