MongoDB使用PHP搜索文档并查找值

问题描述 投票:0回答:1

我有以下json数据:

{ 
"name" : "Template1", 
"data" : {
    "columns" : [ 
        {
            "name" : "Brand Size", 
            "record" : [
                {
                    "fname" : "column_search", 
                    "fields" : [
                        {
                            "name" : "column_search_form_search", 
                            "value" : "332"
                        }, 

                    ], 
                    "rules" : [
                        {
                            "rules_info_cond" : "if", 
                        }
                    ]
                }, 
        {
            "name" : "vendorArticleName", 
            "record" : [
                {
                    "fname" : "column_search", 

                    "rules" : [
                        {
                            "rules_info_cond" : "if", 
                        }
                    ]
                }, 
        {
            "name" : "Remarks", 
            "record" : [
                {
                    "fname" : "column_information_show", 

                    "rules" : [
                        [
                        ]
                    ]
}, 

}

我想得到data-> columns-> name,其中fname ='column_information_show'我能够找到使用MongoDB但无法用PHP找到。请帮帮我!

php mongodb
1个回答
0
投票
$array = json_decode($jsonData, true);

foreach($array['data']['columns'] as $value) {
    if($value['record']['fname'] == 'column_information_show'){
        return $value;
    }
}

这将遍历您的json数据并返回fname等于column_information_show的第一列。如果要返回多个列,可以将它们添加到数组中,然后再返回。

© www.soinside.com 2019 - 2024. All rights reserved.