0

I'm trying to get an array of the arrays in an inner foreach loop.

My code looks something like this:

foreach ( $parent_terms as $parent_term ) {
    $outer_array = array();

        foreach ( $child_terms as $child_term ) {
            $inner_array = array();
            $inner = 'something';
            $inner_array[] = $inner;
            $outer_array[] = $inner_array;

            print_r($inner_array);
        }

      print_r($outer_array);
}

when I print outer array it starts again for each inner array.

2
  • what it suppose to be $child_terms? how does $parent_terms contains? Commented Feb 12, 2020 at 10:47
  • Because you reinitialize it all the time $outer_array = array();. Can you share a demo of your issue and your expected output? Commented Feb 12, 2020 at 10:56

1 Answer 1

1

Update code:

foreach($parent_terms as $parent_term ) {
        $outer_array = array();
        foreach ( $child_terms as $child_term ) {
            $inner_array = ['something'];
            array_push($outer_array,$inner_array);
        }

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

2 Comments

@flinch85 these are basic concepts in PHP. I am happy that you got your answer :)
What is the difference between OP's code and your code? Maybe I have overlooked something.

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.