实例数组
$arr = [
[
'id' => 8,
'mst' => [
'id' => 9,
'mast' => [
'id' => 10
]
]
],
[
'id' => 11,
'mst' => [
'id' => 12,
'mast' => [
'id' => 13
]
]
],
[
'id' => 14,
'mst' => [
'id' => 15,
]
],
[
'id' => 16
]
];
获取数组某个键下的所有值
//获取id
$arr = $sonCategoriesId->toArray();
//$result = array_column($arr, 'id');
$result = $this->re_array($arr, 'id');
foreach ($result as $k => $v){
if ($v['key'] === 'id'){
$res[] = $v['value'];
}
}
//输出$res结果为
//$res = [8,9,10,11,12,13,14,15,16]
将多维数组转换为一维数组
public static function re_array($array, $p_node_deepth=0, $sub_node_deepth=0)
{
$out = array();
foreach($array as $k=>$v){
if(is_array($v)){
$tmp = array("key"=>$k, "value"=>"--","deepth"=>$p_node_deepth);
$out[] = $tmp;
//数组的话,级联调用
$out = array_merge($out, self::re_array($v, $sub_node_deepth+1, $p_node_deepth+1));
}else{
$tmp = array("key"=>$k, "value"=>$v,"deepth"=>$sub_node_deepth);
$out[] = $tmp;
}
}
return $out;
}