简体   繁体   中英

PHP access array item by key value

I have a php array ($menu) with child items. I would like to loop inside the items that have the same parent ID, for instance [post_parent] => 36

foreach($menu as $m) {

*where post_parent => 36 ???*
echo $m->title;

}

here is my array:

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

)

I know it's pretty basic but I can't find the solution... thanks

The example code you offered can be completed as follows. It will simply echo the titles for every item whose parent is over 36.

foreach($menu as $m) {

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

I suspect that might be looking to group menu items (eg printing every title for items with parent 0, then for parent 1, then for 36, and so forth). If you want to group them and print them together, you could try something like this:

(Modify as needed)

$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'];
    }
}

The above code first groups every menu item by its parent, and prints them together. Is this along the lines of what you needed?

Edit: If each item is stored as an object instead of an array, replace the following:

  • $item['parent'] with $item->parent
  • replace $m['parent'] with $m->parent
  • replace $item['title'] with $item->title

You should use foreach and find post_parent equal 36 with if condition like below

foreach($menu as $m) {

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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