2017-05-15 2 views
0

Je veux aller de ces tableaux:Faire un tableau combiné récursive d'autres tableaux

$array1 = ["x", "y", "z"]; 
$array2 = ["a","b"]; 
$array3 = ["1","2","3","4","5","6"]; 

Pour ce tableau:

$arrayResult = 
array(
    array("x" => array("a" => array(1,2,3,4,5,6), 
         "b" => array(1,2,3,4,5,6)), 
      "y" => array("a" => array(1,2,3,4,5,6), 
         "b" => array(1,2,3,4,5,6)), 
      "z" => array("a" => array(1,2,3,4,5,6), 
         "b" => array(1,2,3,4,5,6))) 
); 

J'ai essayé de faire ce tableau combiné avec des approches de produits cartésiens, mais pas de résultat satisfaisant jusqu'à présent.

Répondre

1

Voici une autre solution sans utiliser la boucle:

$array1 = ["x", "y", "z"]; 
$array2 = ["a","b"]; 
$array3 = ["1","2","3","4","5","6"]; 

$result = array_combine(
    $array1, 
    array_fill(
     0, 
     count($array1), 
     array_combine(
      $array2, 
      array_fill(0, count($array2), $array3) 
     ) 
    ) 
); 

print_r($result); 

est ici the demo

+0

Nous vous remercions de toi r réponse! La complexité ici est que je ne sais pas combien de tableaux me seront fournis. Il est plus comme: '$ array1 = [ "x", "y", "z"];' '$ array2 = [ "a", "b"],' '$ array3 = [ » 1 "," 2 "," 3 "," 4 "," 5 "," 6 "];' '$ arrayN ...' – Laurent

0

utilisation array_fill_keys deux fois pour obtenir le résultat

$result = array_fill_keys(
       $array1, 
       array_fill_keys($array2, $array3) 
); 

Demo on eval.in