phparraysarray-combine

array_combine removed duplicate array


Array_combine remove duplicate array

    <?php
        $a1=array("red","red");
        $a2=array("blue","yellow");
        print_r(array_combine($a1,$a2));
    ?>

This code give output : Array ( [red] => yellow )

But I want output like this: Array ( [red] => blue [red] => yellow )


Solution

  • The Andreas answer is correct. you can do this:

    $a1 = ['red'];
    $a2 = ['blue', 'yellow'];
    $a3 = [];
    foreach($a1 as $item1) {
      foreach($a2 as $item2) {
        $a3[$item1][] = $item2;
      }
    }
    
    print_r($a3);
    

    The output:

    array(1) {
      ["red"]=>
      array(2) {
        [0]=>
        string(4) "blue"
        [1]=>
        string(6) "yellow"
      }
    }