Michael King
Michael King

Reputation: 638

PHP How to add an array inside another array with the same key name rather than replacing the one that exists currently

I know my question is kind of confusing, and also similar to others. But it is a tad different in a few ways.

Basically I have this multi dimensional array in PHP with like 6 "dimensions".

I'm building, let's call it $array1, up from a whole bunch of values, which are all stored in a different array, which we will call $array2. $array2 is organized in a way that when you map it out over 6 array levels it makes sense. so an example of how I would create a value in $array1 from $array2 would be:

$array1[$array2[val1][val2]][$array2[val3][val4]] = $array2[val4][val5];

So we are adding in a value to $array1 one string at a time.

The problem I am having is that, $array2 isn't always organized by subject.

So for example (i will use food) $array2 will have as it's values

fruit
 green
  apple
   grape
cereal
 corn
  wheeties
fruit
 blue
  blueberries

I used some spacing in order to read it better so when fruit comes in the second time, it overrides the first fruit inputted. Instead I would like to merge them so $array1 will look like

fruit
 green
  apple
   grape
 blue
  blueberries
cereal
 corn
  wheeties

Updated Code:

            for($r = 0; $r<=$splitUpLength; $r++) {
                if($r == 0) {

                    if ($splitUp[$r]==$p[$main][$cat1]) {
                    } else {    
                        $cat1++;
                        $p[$main][$cat1] = $splitUp[$r];
                        //echo $p[$main][$cat1];
                        //echo "<br>";

                    }

                }

                if ($r == 1) {
                    $main++;
                    if ($splitUp[$r] == $p[$main][$cat2]) {

                    } else {
                        $cat2++;
                        $p[$main][$cat2] = $splitUp[$r];
                        //echo "-".$p[$main][$cat2];
                        //echo "<br>";
                        if (strpos($p[$main][$cat2],'-') !== false) {
                            $realFoods[$p[$main-1][$cat1]] = $p[$main][$cat2];

                            continue;
                        }

                    }

                }

                if ($r == 2) {
                    $main++;
                    if ($splitUp[$r] == $p[$main][$cat3]) {
                    } else {
                        $cat3++;
                        $p[$main][$cat3] = $splitUp[$r];
                        //echo "--".$p[$main][$cat3];
                        //echo "<br>";
                        if (strpos($p[$main][$cat3],'-') !== false) {
                            $realFoods[$p[$main-2][$cat1]][$p[$main-1][$cat2]] = $p[$main][$cat3];

                            continue;
                        }

                    }
                }

                if ($r == 3) {
                    $main++;
                    if ($splitUp[$r] == $p[$main][$cat4]) {

                    } else {
                        $cat4++;
                        $p[$main][$cat4] = $splitUp[$r];
                        //echo "---".$p[$main][$cat4];
                        //echo "<br>";
                        if (strpos($p[$main][$cat4],'-') !== false) {
                            $realFoods[$p[$main-3][$cat1]][$p[$main-2][$cat2]][$p[$main-1][$cat3]] = $p[$main][$cat4];

                            continue;
                        }

                    }
                }

                if ($r == 4) {
                    $main++;
                    if ($splitUp[$r] == $p[$main][$cat5]) {

                    } else {
                        $cat5++;
                        $p[$main][$cat5] = $splitUp[$r];
                        //echo "----".$p[$main][$cat5];
                        //echo "<br>";
                        if (strpos($p[$main][$cat5],'-') !== false) {
                            $realFoods[$p[$main-4][$cat1]][$p[$main-3][$cat2]][$p[$main-2][$cat3]][$p[$main-1][$cat4]] = $p[$main][$cat5];

                            continue;
                        }

                    }
                }

                if ($r == 5) {
                    $main++;
                    if ($splitUp[$r] == $p[$main][$cat6]) {

                    } else {
                        $cat6++;
                        $p[$main][$cat6] = $splitUp[$r];
                        //echo "-----".$p[$main][$cat6];
                        //echo "<br>";
                        if (strpos($p[$main][$cat6],'-') !== false) {
                            $realFoods[$p[$main-5][$cat1]][$p[$main-4][$cat2]][$p[$main-3][$cat3]][$p[$main-2][$cat4]][$p[$main-1][$cat5]] = $p[$main][$cat6];

                            continue;
                        }

                    }
                }



            }
            $main=0;
            $h++;
            //echo $h." ";
        }

    }

So obviously it's a lot

I'm basically splitting up pieces of data and storing them in the $p array. the data is being split up by catagory, and the actual data piece will look like this:

Babyfood, cereal, rice, with mixed fruit, junior - 100 g (100 g)

So i'm splitting them up by "," and once a "-" is detected, it loads all the relevant info into the main array $realFoods

Because some catagores in the data come up twice, like cereal might come up twice, it replaces the already added cereal items. Instead of replacing, I would like to add everything into that cereal category/ array

decleration of the variables:

$h = 0;
$p = array();
$main=0;
$cat1=0;
$cat2=0;
$cat3=0;
$cat4=0;
$cat5=0;
$cat6=0;
$realFoods=array();

I've been looking a lot. and here is a smaller version of my question:

PHP - Merge duplicate array keys in a multidimensional array

But I need to blow that up to support a lot of dimensions. and I just wouldn't know where to begin :/

Upvotes: 1

Views: 1618

Answers (1)

Ragen Dazs
Ragen Dazs

Reputation: 2170

Try this function to merge and keep your key "subject" array:

http://php.net/manual/en/function.array-merge-recursive.php

By the way PHP will try each element of array and merge than

Upvotes: 0

Related Questions