1

I have 4 arrays [$category_id => $parent_id, ...]:

$depth0 = [
    1 => 0, 
    2 => 0,
    3 => 0,
    4 => 0,
    5 => 0                      
    ];

$depth1 = [
    10 => 1,
    11 => 1,
    12 => 1,
    13 => 2,
    14 => 2,
    15 => 3,
    16 => 3
];

$depth2 = [
    17 => 11,
    18 => 11,
    19 => 11,
    20 => 13,
    21 => 13,
    22 => 14                        
];

$depth3 = [
    23 => 20,
    24 => 20
];

I need merge them all. Merge $depth0 with $depth1 should give me:

1 => [10 =>[], 11=>[], 12=>[]], 
2 => [13 =>[], 14=>[]],
3 => [14=>[], 15=>[], 16=>[]],
4 => [],
5 => []

next with $depth2:

1 => [10 =>[], 11=>[17=>[], 18=>[], 19=>[]], 12=>[]], 
2 => [13 =>[20=>[], 21=>[]], 14=>[]],
3 => [14=>[22=>[]], 15=>[], 16=>[]],
4 => [],
5 => []

and finally with $depth3:

$result = [
    1 => [10 =>[], 11=>[17=>[], 18=>[], 19=>[]], 12=>[]], 
    2 => [13 =>[20=>[23=>[], 24=>[]], 21=>[]], 14=>[]],
    3 => [14=>[22=>[]], 15=>[], 16=>[]],
    4 => [],
    5 => []                      
];

I was trying everything to done it, but with no success and I really don't have any other ideas, please help me (array_merge doesn't work).

I think hear I'm close (I'm using Code Igniter - PHP Framework, Not all could be clear, but I think description above should be enought):

//$categories - array with 4 SQL questions for 4 depth of categories
    private function _prepare_categories($categories) { 
        $categories_array = array();
        $empty_array = array();
        foreach ($categories as $depth => $query) {
            foreach ($query->result() as $row) {

                if($depth == 0) {
                    $categories_array[$row->ID_category] = $empty_array;
                }
                else {  
                    $categories_array = $this->multiKeyExists($categories_array, $row->ParentID, $row->ID_category);
                } 
            }

        }
        return $categories_array;
    }

    public function multiKeyExists(array $array, $parent_id, $id_category) {
        // is in base array?
        if (array_key_exists($parent_id, $array)) {
            $array[$parent_id][$id_category] = array();
            return $array;
        }
        // check arrays contained in this array
        foreach ($array as $element) {
            if (is_array($element)) {
                $new_array = $this->multiKeyExists($element, $parent_id, $id_category);
                if(!empty($new_array)) {
                    $array = array_merge($array, $element);
                    return $array;
                }
            }
        }
        return $array;
    }
4
  • 1
    show your examples of what you've tried - you mention array_merge Commented Jul 6, 2016 at 17:19
  • dbmitch I added edit with my example. Commented Jul 6, 2016 at 17:37
  • Thanks - and what happens when you use your example - what do resulting arrays look like? Commented Jul 6, 2016 at 17:43
  • problem is in array_merge - this function reindexing all my keys in arrays (witch are category IDs), so I got new array with indexing from 0 to 18. If my keys would be strings, my solution should work Commented Jul 6, 2016 at 17:49

1 Answer 1

1
$res = [];

foreach([$depth3, $depth2, $depth1, $depth0] as $depth)  
   foreach($depth as $k => $i) {
      if(isset($res[$k])) { $res[$i][$k] = $res[$k]; unset($res[$k]); }
      else $res[$i][$k] = [];
   }

print_r($res[0]);

demo

Sign up to request clarification or add additional context in comments.

2 Comments

loop them in order from the deeper to upper. If structure will not change, itshould work
Man, you are my hero! I can't believe it was that easy. Thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.