2

I have two arrays:

Parents:

Array
(
    [0] => Levi Self
    [2] => Portraits
    [3] => Abstract
)

Children:

Array
(
    [0] => Portraits
    [1] => Abstract
    [2] => Megan
    [3] => Locks
)

And I'm trying to build an array that looks like this:

Array
(
    [Levi Self] => Array
      (
          [0] => Portraits
          [1] => Abstract
      )

    [Portraits] => Array
      (
          [0] => Megan
      )

     [Abstract] => Array
      (
          [0] => Locks
      )
)

I am getting the hierarchy from the database table that looks like this:

Array
(
    [0] => Array
        (
            [title] => Portraits
            [parent] => Levi Self
        )

    [1] => Array
        (
            [title] => Abstract
            [parent] => Levi Self
        )

    [2] => Array
        (
            [title] => Megan
            [parent] => Portraits
        )

    [3] => Array
        (
            [title] => Locks
            [parent] => Abstract
        )

)

Am I trying something that is impossible? Thanks, Levi Self

1 Answer 1

5
foreach ($that_last_array_of_yours as $arr) {
  $result[$arr['parent']][] = $arr['title'];
}

print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

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.