最佳答案
Here is your desired solution.
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);
}
}
}}
Using 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('
');
print_r($category_padding_array);
echo('
');
参考答案2
Try this
$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;
}
参考答案3
If you look at this example, you should be able to figure out that all you need to do is replace the array name and values to accommodate your requirements. Clearly your data is not numbers so you need to find out for yourself how you would deal with the vehicle names.
$tmpArray = array(array("one",array(1,2,3)),array("two",array(4,5,6)),array("three",array(7,8,9)));
And use print to check the structure
print_r($tmpArray);
参考答案4
You can do it with recursive calling of custom function or like this:
$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);
参考答案5
For creating a tree array use a Recursive function. This is working perfectly for me :-
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 '
', print_r($tree, TRUE);