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] => 
        )

)

我知道这很基本,但我找不到解决方案...谢谢

解决方法

您提供的示例代码可以按如下方式完成。它将简单地回显父项超过 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'];
    }
}

上面的代码首先将每个菜单项按其父项分组,然后将它们打印在一起。这是否符合您的需求?

编辑:如果每个项目都存储为对象而不是数组,请替换以下内容:

  • $item['parent'] 与 $item->parent
  • 将 $m['parent'] 替换为 $m->parent
  • 用 $item->title 替换 $item['title']
,

你应该使用 foreach 并找到等于 36 的 post_parent 和 if 条件如下

foreach($menu as $m) {

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