Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...