-2

I have an array like:

array(
    0 => array("a", "b", "c", ... n),
    1 => array("a", "b", "d", ... n),
    2 => array("e", "b", "c", ... n),
    .
    n
    );

With n lenght right and down. I want an tree array like:

a {
    b {
        c {null}
        d {null}
       }
  }
e {
   b {
      c {null}
     }
  }

Extactly, something like: How to convert array to tree? but in PHP code and for 'n' lenght array

My code, translated from javascript:

$tree = array();

function addToTree($tree, $array) {
    $length = count($array);
    for($i=0; $i<$length; $i++) {
        $tree = $tree[$array[$i]] = (($i == $length - 1) ? null : $tree[$array[$i]] || array());
    }
    return $tree;
}
6
  • Can you clarify what your tree array needs to look like. What you show does not look right. Commented Mar 14, 2017 at 17:11
  • 3
    Stack Overflow is not a free code writing service. You are expected to try to write the code yourself. After doing more research if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a Minimal, Complete, and Verifiable example. I suggest reading How to Ask a good question and the perfect question. Also, be sure to take the tour. Commented Mar 14, 2017 at 17:12
  • Maybe this question helps you: stackoverflow.com/questions/32647745/… Commented Mar 14, 2017 at 17:16
  • As I see from the referred question the question is in same manner as that so is there A change in questions asking technique ? @JohnConde Commented Mar 14, 2017 at 17:25
  • 1
    @IshanMahajan Seven years ago the standards at this site were much more lax. It has been clarified that questions like these are too broad and should be closed as such. Commented Mar 14, 2017 at 17:28

1 Answer 1

0

The answer is same as Christians answer just you have to edit it for php right syntax.

function addToTree($tree, $array) { 
    for ($i = 0, $length = count($array); $i < $length; $i++) {
        $tree = $tree[array[$i]] = (($i == $length - 1) ? null : $tree[array[$i]] || {})
    }
}

// or, without the i == length - 1 check in each iteration:
function addToTree(tree, array) { 
    for ($i = 0, $length = count($array); $i < $length -1; $i++) {
        $tree = $tree[array[$i]] = $tree[array[$i]] || {};
    } 
    $tree[array[$i]] = null;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don't undestand this: || {} (or empty keys? how?)
I don't understand this ternary operator in javascript and how to translate it to PHP tree = tree[array[i]] = ((i == length - 1) ? null : tree[array[i]] || {})
My translated code is this, but it doesn't works. function addToTree($tree, $array) { $length = count($array); for($i=0; $i<$length; $i++) { $tree = $tree[$array[$i]] = (($i == $length - 1) ? null : $tree[$array[$i]] || array()); } return $tree; }
Someone coul explain me this line? $tree = $tree[array[$i]] = (($i == $length - 1) ? null : $tree[array[$i]] || {}) I'm triying to translate it to PHP but i don't undestand it. Thaks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.