-1

I am trying to merge 2 arrays a single array on a multidimensional array where a given key-value = a value

the first array looks like this:

Array
(
  [0] => Array
    (
        [id] => 4
        [subcategories] => Array
            (
                [0] => Array
                    (
                        [id] => 5
                        [category_order] => 0
                        [parent_id] => 4
                        [name] => Audio Equipment
                    )

                [1] => Array
                    (
                        [id] => 6
                        [category_order] => 0
                        [parent_id] => 4
                        [name] => Home Entertainment
                    )

                [2] => Array
                    (
                        [id] => 7
                        [category_order] => 0
                        [parent_id] => 4
                        [name] => Photography
                    )

                [3] => Array
                    (
                        [id] => 8
                        [category_order] => 0
                        [parent_id] => 4
                        [name] => Portable Audio
                    )

                [4] => Array
                    (
                        [id] => 9
                        [category_order] => 0
                        [parent_id] => 4
                        [name] => Televisions
                    )

            )

    )

)

and the second like this:

Array
(
[0] => Array
    (
        [id] => 10
        [parent_id] => 5
        [name] => Amplifiers & Receivers
    )

[1] => Array
    (
        [id] => 11
        [parent_id] => 5
        [name] => Audio Systems
    )

[2] => Array
    (
        [id] => 12
        [parent_id] => 5
        [name] => Cassette Decks
    )

[3] => Array
    (
        [id] => 13
        [parent_id] => 5
        [name] => CD Players
    )

[4] => Array
    (
        [id] => 14
        [parent_id] => 5
        [name] => Radios
    )

[5] => Array
    (
        [id] => 15
        [parent_id] => 5
        [name] => HiFi Speakers
    )
)

What I want to do is add each of the second arrays to a sub array of the first multidimensional array where the parent_id of the second array = the id of the subcategories array of the first array so it will look like this:

array
(
   [0]=> Array
   (
     [id] => 4
      [subcategories] => Array
        (
         [0] => Array
         (
             [id] => 5
             [category_order] => 0
             [parent_id] => 4
             [name] => Audio Equipment
             [subsubcategories] = array 
               (
                [id] => 10
                [parent_id] => 5
                [name] => Amplifiers & Receivers
             )
         )
Adrian Brown
  • 79
  • 1
  • 8

1 Answers1

1

Something like this should work just rename the array names because you didn't provide them. But I think you'll get the idea :) Mainly you loop through all subcategories with foreach loop or another you'll get the parent id and can access the main array with that parent id and save the sub categories info in there.

foreach( $sub_array as $item ) {
     $main_array[ category_id ][ $item[ 'parent_id' ] ][ 'subsubcategories' ] = $item;
}
  • Hi Thanks for your reply, surely that would just assign the $sub_array item to the $main_array location not the parent id? main_array[category_id][0][subcategories] = array() I would think it would need some sort of nested loop and a condition to find out if the sub_array parent_id matched the main_array category_id? – Adrian Brown Jan 19 '15 at 10:32