fork(1) download
  1. <?php
  2.  
  3. $example = array(
  4. 'id' => 63,
  5. 'parentid' => 0,
  6. 'char_value' => 'End poverty in all its forms everywhere',
  7. 'param_value' => 1,
  8. 'children' => array(
  9. 'id' => 84,
  10. 'parentid' => 63,
  11. 'char_value' => 'test 1',
  12. 'param_value' => 1
  13. ),
  14. 'id' => 85,
  15. 'parentid' => 63,
  16. 'char_value' => 'test 2',
  17. 'param_value' => 1
  18. )
  19. )
  20. )
  21. );
  22.  
  23. print_r($example);
  24.  
  25. function drawPropertyTree($array, $parent)
  26. {
  27. $result = array();
  28. foreach ($array as $k => $v) {
  29.  
  30. $pieces = explode(" ", $v['char_value'], 6);
  31. $name = implode(" ", array_splice($pieces, 0, 3));
  32.  
  33. $result[] = array(
  34. 'id' => $v['id'],
  35. 'parent' => $parent,
  36. 'text' => $v['param_value'] . " " . $name
  37. );
  38.  
  39. if (isset($v['children'])) {
  40. $result[] = drawPropertyTree($v['children'], $v['id']);
  41. }
  42. }
  43. return $result;
  44. }
  45.  
  46. print_r(drawPropertyTree($example, 0));
Success #stdin #stdout 0.03s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [id] => 63
            [parentid] => 0
            [char_value] => End poverty in all its forms everywhere
            [param_value] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 84
                            [parentid] => 63
                            [char_value] => test 1
                            [param_value] => 1
                        )

                    [1] => Array
                        (
                            [id] => 85
                            [parentid] => 63
                            [char_value] => test 2
                            [param_value] => 1
                        )

                )

        )

)
Array
(
    [0] => Array
        (
            [id] => 63
            [parent] => 0
            [text] => 1 End poverty in
        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 84
                    [parent] => 63
                    [text] => 1 test 1
                )

            [1] => Array
                (
                    [id] => 85
                    [parent] => 63
                    [text] => 1 test 2
                )

        )

)