M.Aldein
M.Aldein

Reputation: 41

Combine multiple arrays with identica keys and different values

I have this two arrays that are generated in two foreach loops and I want to set the first array as keys and the second one as values.

after I use this code

foreach ($difference AS $j) {
   $fv = $cate->getFilterValueByFeatureID($j);
        foreach ($fv AS $z) {
             $array = array(
                  $j => $z
                 );
              var_dump($array);
          }
 }

this is what I get

array(1) {
  [6]=>
  int(15)
}

array(1) {
   [6]=>
   int(20)
 }

array(1) {
    [8]=>
  int(26)
}

array(1) {
  [8]=>
  int(27)
    }

array(1) {
    [8]=>
    int(33)
}

and I want this result

array(1){
[6] => array(
        [0] => 15
        [1] => 20
)
array(1){
[8] => array(
        [0] => 26
        [1] => 27
        [2] => 33
)

Upvotes: 0

Views: 38

Answers (1)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

Like this (untested)

$result = [];
foreach ($difference AS $j) {
  $fv = $cate->getFilterValueByFeatureID($j);
    foreach ($fv AS $z) {
         if(!isset($result[$j])) $result[$j] = [];

         $result[$j][] = $z;
      }
}

var_dump($result);

Upvotes: 1

Related Questions