怎么去掉PHP多维数组里的key?

Geomen 发布于 03/07 11:46
阅读 1K+
收藏 1
PHP

有这么一个多维数组

{
    "110000": {
        "value": "110000", 
        "label": "北京市", 
        "children": {
            "110100": {
                "value": "110100", 
                "label": "北京市", 
                "children": {
                    "110101": {
                        "value": 110101, 
                        "label": "东城区"
                    }
                }
            }
        }
    }, 
    "350000": {
        "value": "350000", 
        "label": "福建省", 
        "children": {
            "350200": {
                "value": "350200", 
                "label": "厦门市", 
                "children": {
                    "350203": {
                        "value": 350203, 
                        "label": "思明区"
                    }, 
                    "350206": {
                        "value": 350206, 
                        "label": "湖里区"
                    }
                }
            }
        }
    }, 
    "510000": {
        "value": "510000", 
        "label": "四川省", 
        "children": {
            "510700": {
                "value": "510700", 
                "label": "绵阳市", 
                "children": {
                    "510703": {
                        "value": 510703, 
                        "label": "涪城区"
                    }
                }
            }
        }
    }
}

请问怎么把数组里的key都去掉?只保留'label','value','children'!

加载中
1
周小君
周小君
        $str = '{
                "110000": {
                    "value": "110000", 
                    "label": "北京市", 
                    "children": {
                        "110100": {
                            "value": "110100", 
                            "label": "北京市", 
                            "children": {
                                "110101": {
                                    "value": 110101, 
                                    "label": "东城区"
                                }
                            }
                        }
                    }
                }, 
                "350000": {
                    "value": "350000", 
                    "label": "福建省", 
                    "children": {
                        "350200": {
                            "value": "350200", 
                            "label": "厦门市", 
                            "children": {
                                "350203": {
                                    "value": 350203, 
                                    "label": "思明区"
                                }, 
                                "350206": {
                                    "value": 350206, 
                                    "label": "湖里区"
                                }
                            }
                        }
                    }
                }, 
                "510000": {
                    "value": "510000", 
                    "label": "四川省", 
                    "children": {
                        "510700": {
                            "value": "510700", 
                            "label": "绵阳市", 
                            "children": {
                                "510703": {
                                    "value": 510703, 
                                    "label": "涪城区"
                                }
                            }
                        }
                    }
                }
            }';
        $arr = json_decode($str,true);
        $arr = array_values($arr);
        foreach ($arr as &$item){
            $item['children'] = array_values($item['children']);
            foreach ($item['children'] as &$child){
                $child['children'] = array_values($child['children']);
            }
        }
        echo json_encode($arr);

这个应该就是你想要的。

Geomen
Geomen
谢谢!
0
Geomen
Geomen

引用来自“mickelfeng”的评论

array_values

arrayv_values只能去掉最外层的吧!

我需要把里面的也去掉哦!

0
周小君
周小君

能把你最终想要的结果贴出来看看么

0
Geomen
Geomen

引用来自“周小君”的评论

能把你最终想要的结果贴出来看看么

我是用到的element-ui里的Cascader 级联选择器

里面的例子的数据有点多,我就贴链接吧:http://element-cn.eleme.io/#/zh-CN/component/cascader

 

0
lbaobin
lbaobin
<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/3/29
 * Time: 18:33
 */
$json_str = '{
    "110000": {
        "value": "110000", 
        "label": "北京市", 
        "children": {
            "110100": {
                "value": "110100", 
                "label": "北京市", 
                "children": {
                    "110101": {
                        "value": 110101, 
                        "label": "东城区"
                    }
                }
            }
        }
    }, 
    "350000": {
        "value": "350000", 
        "label": "福建省", 
        "children": {
            "350200": {
                "value": "350200", 
                "label": "厦门市", 
                "children": {
                    "350203": {
                        "value": 350203, 
                        "label": "思明区"
                    }, 
                    "350206": {
                        "value": 350206, 
                        "label": "湖里区"
                    }
                }
            }
        }
    }, 
    "510000": {
        "value": "510000", 
        "label": "四川省", 
        "children": {
            "510700": {
                "value": "510700", 
                "label": "绵阳市", 
                "children": {
                    "510703": {
                        "value": 510703, 
                        "label": "涪城区"
                    }
                }
            }
        }
    }
}';

$data = json_decode($json_str,1);

function filter_data($data){
    $data = array_values($data);
    foreach ($data as &$item){
        if(isset($item['children'])){
            $item['children'] = filter_data($item['children']);
        }
    }
    return $data;
}
$data = filter_data($data);
echo json_encode($data);

 

0
F
Fly_boy_Hss
/**
  * 使用递归来处理
  * @params array $array
  */
function format(array &$array)
{
    if ($array) {
        $array = array_values($array);
        foreach ($array as &$item) {
            if (isset($item['children'])) {
                $item['children'] = format($item['children']);
            }
        }
    }
    return $array;
}

 

返回顶部
顶部