array(3) { ["name"]=> array(1) { [0]=> ...">

Flatten multidimensional array but keep keys?

I have the following:

[6199]=>
  array(12) {
    ["Origin"]=>
    array(3) {
      ["name"]=>
      array(1) {
        [0]=>
        string(4) "Cuba"
      }
      ["slug"]=>
      array(1) {
        [0]=>
        string(27) "cuabn-havana-habanos-cigars"
      }
      ["id"]=>
      array(1) {
        [0]=>
        int(0)
      }
    }
    ["Filler"]=>
    array(3) {
      ["name"]=>
      array(2) {
        [0]=>
        string(9) "Dominican"
        [1]=>
        string(10) "Nicaraguan"
      }
      ["slug"]=>
      array(2) {
        [0]=>
        string(9) "dominican"
        [1]=>
        string(10) "nicaraguan"
      }
      ["id"]=>
      array(2) {
        [0]=>
        int(0)
        [1]=>
        int(1)
      }
    }
  }
  [6192]=>
  array(11) {
    ["Origin"]=>
    array(3) {
      ["name"]=>
      array(1) {
        [0]=>
        string(9) "Nicaragua"
      }
      ["slug"]=>
      array(1) {
        [0]=>
        string(27) "nicaraguan-new-world-cigars"
      }
      ["id"]=>
      array(1) {
        [0]=>
        int(0)
      }
    }
    ["Filler"]=>
      array(3) {
        ["name"]=>
        array(2) {
          [0]=>
          string(9) "Java"
          [1]=>
          string(10) "Nicaraguan"
        }
        ["slug"]=>
        array(2) {
          [0]=>
          string(9) "java"
          [1]=>
          string(10) "nicaraguan"
        }
        ["id"]=>
        array(2) {
          [0]=>
          int(0)
          [1]=>
          int(1)
        }
      }
  }

      

and my expected output:

  array(12) {
    ["Origin"]=>
    array(3) {
      ["name"]=>
      array(1) {
        [0]=>
        string(4) "Cuba".
        [1]=>
        string(9) "Nicaragua"
      }
      ["slug"]=>
      array(1) {
        [0]=>
        string(27) "cuabn-havana-habanos-cigars",
        [0]=>
        string(27) "nicaraguan-new-world-cigars"
      }
      ["id"]=>
      array(1) {
        [0]=>
        int(0)
      }
    }
    ["Filler"]=>
    array(3) {
      ["name"]=>
      array(2) {
        [0]=>
        string(9) "Dominican"
        [1]=>
        string(10) "Nicaraguan"
        [2]=>
        string(9) "Java"
      }
      ["slug"]=>
      array(2) {
        [0]=>
        string(9) "dominican"
        [1]=>
        string(10) "nicaraguan"
        [3]=>
        string(9) "java"
      }
      ["id"]=>
      array(2) {
        [0]=>
        int(0)
        [1]=>
        int(1)
      }
    }

      

See how it fixes errors and concatenates each array that supports the "origin" key. I tried:

  foreach ($resultterms as $keyname => $valuename){

    foreach ($valuename as $keysub => $valuesub) {

      foreach($valuesub['name'] as $keysubsub => $valuesubsub){

        # code...
        $prods_atts[$keysub]['name'][$keysubsub] = $valuesubsub;
        $prods_atts[$keysub]['slug'][$keysubsub] = $valuesub['slug'][$keysubsub];
        $prods_atts[$keysub]['id'][$keysubsub] = $valuesub['id'][$keysubsub];

      }
    }

  }

      

where $resultterms

are the original arrays, but it doesn't work. I was wondering if there is a great php function I could use to combine them, rather than nesting so many for each loop?

+3


source to share


2 answers


I believe you are just looking for array_merge_recursive .

call_user_func_array('array_merge_recursive', array_values($prod_atts));

      



To try it out, could you please place var_export

your variable instead var_dump

?

echo(var_export($prod_atts, true));

      

+2


source


concatenate the array with any suggested method. After that, you will get duplicate values. And you only need to keep unique items



$new = array_merge_recursive($resultterms['6199'], $resultterms['6192']);

foreach($new['Origin'] as &$item) { $item = array_unique($item); }
foreach($new['Filler'] as &$item) { $item = array_unique($item); }

      

+1


source







All Articles