创建一个树数组PHP

时间:2015-10-27 06:48:19

标签: php arrays tree

我有一个数组:

$data_array = array(
        array('id'=> 1 , 'parent_id' => 1 , 'name'=>'Air Vehicles' ),
        array('id'=> 2 , 'parent_id' => 2 , 'name'=>'Land Vehicles' ),
        array('id'=> 3 , 'parent_id' => 3 , 'name'=>'Water Vehicles' ),
        array('id'=> 4 , 'parent_id' => 2 , 'name'=>'Bikes' ),
        array('id'=> 5 , 'parent_id' => 2 , 'name'=>'Cars' ),
        array('id'=> 6 , 'parent_id' => 1 , 'name'=>'Aero Planes'),
        array('id'=> 7 , 'parent_id' => 1 , 'name'=>'Helicopter'),
        array('id'=> 8 , 'parent_id' => 3 , 'name'=>'Ships'),
        array('id'=> 9 , 'parent_id' => 1 , 'name'=>'Hoverboard'),
        array('id'=> 10 , 'parent_id' => 2 , 'name'=>'Hoverboard'),
        array('id'=> 11 , 'parent_id' => 4 , 'name'=>'R1 Kawasaki'),
        array('id'=> 12 , 'parent_id' => 4 , 'name'=>'Suzuki Hayabusa'),
        );

我想从数组上面创建一个树数组:

Air Vechiles
-Aero Planes
-Helicopter
-HoverBoard
Land Vechiles
-Bikes
--R1 Kawasaki
--Suzuki Hayabusa
-Cars
Water Vehicles
-Ships

5 个答案:

答案 0 :(得分:2)

这是您理想的解决方案。

class CategoryTree{

public $padding_array;  

public function __construct(){
    $this->padding_array = array();
}

public function getTreeArray($data_array){
    foreach($data_array as $i=>$data){
        if($data['id'] == $data['parent']) $data_array[$i]['parent'] = 0;
    }
    return $this->getCategoryTreeArray($data_array);
}

public function getCategoryTreeArray($elements, $parent_id = 0){
    $branch = array();
    foreach ($elements as $element) {
        if ($element['parent'] == $parent_id) {
            $children = $this->getCategoryTreeArray($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[$element['id']] = $element;
        }
    }
    return $branch;
}

public function getCatgoryPadingArray($tree_array, array &$padding_array, $depth=0){
    foreach ($tree_array as $id => $data_array) {
            $padding_array[$id] = str_repeat('-', $depth).$data_array['name'];
            if(is_array($data_array) && array_key_exists('children', $data_array)){
                $this->getCatgoryPadingArray($data_array['children'], $padding_array , $depth+1);
            }
    }
}}

使用class:

$data_array  = array(); // your array data
$tree_object = new CategoryTree();
$category_tree_array = $tree_object->getTreeArray($data_array);
$tree_object->getCatgoryPadingArray($category_tree_array, $tree_object->padding_array);
$category_padding_array = $tree_object->padding_array;
echo('<pre>');
print_r($category_padding_array);
echo('<pre>');

答案 1 :(得分:0)

试试这个

$new = array();
foreach ($data_array as $a){
$new[$data_array['parentid']][] = $a;
}
$tree = createTree($new, array($data_array[0]));
print_r($tree);

function createTree(&$list, $parent){
$tree = array();
foreach ($parent as $k=>$l){
    if(isset($list[$l['id']])){
        $l['children'] = createTree($list, $list[$l['id']]);
    }
    $tree[] = $l;
} 
return $tree;
}

答案 2 :(得分:0)

如果你看一下这个例子,你应该能够弄清楚你需要做的就是替换数组名称和值以满足你的要求。显然,您的数据不是数字,因此您需要自己了解如何处理车辆名称。

  $tmpArray = array(array("one",array(1,2,3)),array("two",array(4,5,6)),array("three",array(7,8,9)));

并使用print检查结构

  print_r($tmpArray);

答案 3 :(得分:0)

您可以通过递归调用自定义函数来执行此操作,或者像这样:

$data_array = array(
    array('id' => 1, 'parent_id' => 1, 'name' => 'Air Vehicles'),
    array('id' => 2, 'parent_id' => 2, 'name' => 'Land Vehicles'),
    array('id' => 3, 'parent_id' => 3, 'name' => 'Water Vehicles'),
    array('id' => 4, 'parent_id' => 2, 'name' => 'Bikes'),
    array('id' => 5, 'parent_id' => 2, 'name' => 'Cars'),
    array('id' => 6, 'parent_id' => 1, 'name' => 'Aero Planes'),
    array('id' => 7, 'parent_id' => 1, 'name' => 'Helicopter'),
    array('id' => 8, 'parent_id' => 3, 'name' => 'Ships'),
    array('id' => 9, 'parent_id' => 1, 'name' => 'Hoverboard'),
    array('id' => 10, 'parent_id' => 2, 'name' => 'Hoverboard'),
    array('id' => 11, 'parent_id' => 4, 'name' => 'R1 Kawasaki'),
    array('id' => 12, 'parent_id' => 4, 'name' => 'Suzuki Hayabusa'),
);
$tree = array();
foreach ($data_array as $k => &$val) {
    if ($val['parent_id'] == $val['id']) {
        if (empty($tree[$val['parent_id']])) {
            $tree[$val['parent_id']] = array();
        }
        $tree[$val['parent_id']] = array_merge($tree[$val['parent_id']], $val);
    } else {
        $tree[$val['parent_id']]['sub'][] = $val;
    }
}
print_r($tree);

答案 4 :(得分:0)

要创建树阵列,请使用递归函数。这对我来说非常有用: -

foreach($data_array as $i=>$data)
{
    if($data['id'] == $data['parent_id']) $data_array[$i]['parent_id'] = 0;
}

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;
}

$tree = buildTree($data_array);

echo '<pre>', print_r($tree, TRUE);