Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten multi dimension array but maintain keys?

Tags:

arrays

php

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 is:

  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 eliminates dupes and merges each array maintaining the "origin" key. I've 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 is the original arrays but it's not working. I was wondering if there was a wonderful php function I could use to merge these instead of so many nested for each loops?

like image 804
vimes1984 Avatar asked Aug 08 '26 20:08

vimes1984


2 Answers

I believe you're just looking for array_merge_recursive.

call_user_func_array('array_merge_recursive', array_values($prod_atts));
  • call_user_func_array allows to transform an array into a list of arguments
  • array_values because in the end, you seem to want to get rid of the first layer of your array

In order to try it, could you post the var_export of your variable instead of the var_dump?

echo(var_export($prod_atts, true));
like image 194
Jerska Avatar answered Aug 11 '26 09:08

Jerska


merge your array by any suggested method. After that you will get duplicated values. And you need save only the 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); }
like image 25
splash58 Avatar answered Aug 11 '26 10:08

splash58



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!