首页 > 解决方案 > MongoDB 使用 PHP 搜索文档并查找值

问题描述

我有以下 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" : [
                        [
                        ]
                    ]
}, 

}

我想获取具有 fname = 'column_information_show' 的 data->columns->name 我能够使用 MongoDB 找到但无法使用 PHP 找到。请帮帮我!

标签: phpmongodb

解决方案


$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 的第一列。如果要返回多列,可以将它们添加到数组中,然后稍后返回。


推荐阅读