用PHP在嵌套数组中转换普通数组 [英] convert normal array in nested array with php

查看:68
本文介绍了用PHP在嵌套数组中转换普通数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的分层数据库表

  ID名称子类别ParentID 
1 ABC 0
2 DEF QFE 0
3 QFE XYZ 2
4 XYZ MNJ 3

从那我有下面的PHP数组

  $ array_name = array(
array('ID'=>' 1','Name'=>'ABC','Subcategory'=>'','ParentID'=>'0'),
array('ID'=>'2',' Name'=>'DEF','Subcategory'=>'QFE','ParentID'=>'0'),
array('ID'=>'3','Name'= >'QFE','Subcategory'=>'XYZ','ParentID'=>'2'),
array('ID'=>'4','Name'=>' XYZ'','Subcategory'=> gt;'MNJ','ParentID'=>'3')
);

但我想要下面的数组

  $ array_name = array(
array('ID'=>'1','Name'=>'ABC','Subcategory'=>'',' ParentID'=>'0'),
array('ID'=>'2','Name'=>'DEF','Subcategory'=> array('ID'=> '3','名称'=>'QFE','子类别'=> array('ID'=>'4','名称'=>'XYZ','子类别'=>'MNJ ','ParentID'=>'3'),'ParentID'=>'2'),'ParentID'=>'0'),
);

我想要一个函数,该函数检查该行是否具有子类别,如果某行具有子类别,则将该子类别行作为数组获取,并使用所有类别和子类别创建一个数组



为此,我试图制作一个下面给出的函数

 函数find_subcategory($ ID,$ con){
$ table_name = SELECT * FROM`table_name` WHERE`parent_id` ='$ ID';
$ table_name_result = mysqli_query($ con,$ table_name);
$ category_array = array();
if(mysqli_num_rows($ table_name_result)){
while($ row = mysqli_fetch_assoc($ table_name_result)){
$ Subcategory = $ row [’Subcategory’];
$ ID = $ row ['ID'];
if($ Subcategory ==’){
$ find_subcategory = find_subcategory($ ID,$ con);
$ row ['Subcategory'] = $ find_subcategory;
$ category_array [] = $ row;
} else {
$ category_array [] = $ row;
}

}
}
返回json_encode(array(’tbl_category’=> $ category_array));
}

,但是此功能无法获得一个类别的所有子类别。 / p>

有人可以帮我吗

解决方案

我尝试了以下功能并且正在运行

  function select_all($ con){
$ table_name = SELECT * FROM`Category` ;
$ table_name_result = mysqli_query($ con,$ table_name);
$ array = mysqli_fetch_all($ table_name_result,MYSQLI_ASSOC);
返回$ array;
}

函数buildTree(array $ elements,$ parentId = 0){
$ branch = array();

foreach($ elements as $ element){
if($ element ['parent_id'] == $ parentId){
$ children = buildTree($ elements,$ element ['ID']);
if($ children){
$ element [’children’] = $ children;
}
$ branch [] = $ element;
}
}

return $ branch;
}
$ select_all = select_all($ con);
$ subcategory = buildTree($ select_all);
echo json_encode(array_map( utf8_converter,$ subcategory));

这可以正常工作。


I have a hierarchical database table like below

ID  Name    Subcategory ParentID
1   ABC                     0
2   DEF         QFE         0
3   QFE         XYZ         2
4   XYZ         MNJ         3

From Thant I have got PHP array like below

$array_name =  array(
                array('ID' => '1', 'Name' => 'ABC', 'Subcategory' => '', 'ParentID' => '0'),
                array('ID' => '2', 'Name' => 'DEF', 'Subcategory' => 'QFE', 'ParentID' => '0'),
                array('ID' => '3', 'Name' => 'QFE', 'Subcategory' => 'XYZ', 'ParentID' => '2'),
                array('ID' => '4', 'Name' => 'XYZ', 'Subcategory' => 'MNJ', 'ParentID' => '3')
            );

but I want array like below

$array_name = array(
                array('ID' => '1', 'Name' => 'ABC', 'Subcategory' => '', 'ParentID' => '0'),
                array('ID' => '2', 'Name' => 'DEF', 'Subcategory' => array('ID' => '3', 'Name' => 'QFE', 'Subcategory' => array('ID' => '4', 'Name' => 'XYZ', 'Subcategory' => 'MNJ', 'ParentID' => '3'), 'ParentID' => '2'), 'ParentID' => '0'),                   
                );

I want a function which checks is that row have some Subcategory or not and if a row has Subcategory then get that subcategory row as an array and make one array with all category and Subcategory

for that, I have tried to make a function which is given below

function find_subcategory($ID,$con){
$table_name ="SELECT * FROM `table_name` WHERE `parent_id` = '$ID'";
$table_name_result = mysqli_query($con,$table_name);
$category_array = array();
if(mysqli_num_rows($table_name_result)) {
    while($row = mysqli_fetch_assoc($table_name_result)) {
        $Subcategory= $row['Subcategory'];
        $ID = $row['ID'];
        if ($Subcategory== '') {
            $find_subcategory = find_subcategory($ID,$con);
            $row['Subcategory'] = $find_subcategory;
            $category_array[] = $row;
        }else{
            $category_array[] = $row;
        }

    }
}   
return json_encode(array('tbl_category'=>$category_array));
}

but this function is not working to get all the subcategories of one category.

can anybody help me with this

解决方案

I have tried below function and it is working

function select_all($con){
    $table_name ="SELECT * FROM `Category`";
    $table_name_result = mysqli_query($con,$table_name);
    $array = mysqli_fetch_all($table_name_result, MYSQLI_ASSOC);
    return $array;
}   

function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['ID']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}
$select_all = select_all($con);
$subcategory = buildTree($select_all);
echo json_encode(array_map("utf8_converter",$subcategory));

and this is working correctly.

这篇关于用PHP在嵌套数组中转换普通数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆