Existe-t-il un moyen de combiner plusieurs tableaux avec le même nombre d'éléments? Voir ci-dessous les 4 tableaux avec 5 éléments à l'intérieur:
$type = array('Type 1','Type 2','Type 3',' Type 4','Type 5');
$desc = array('Description 1','Description 2','Description 3','Description 4','Description 5');
$brand = array('Brand 1','Brand 2','Brand 3','Brand 4','Brand 5');
$model = array('Model 1','Model 2','Model 3','Model 4','Model 5');
Résultats attendus:
[0] => Array
(
[0] => Type 1
[1] => Description 1
[2] => Brand 1
[3] => Model 1
)
[1] => Array
(
[0] => Type 2
[1] => Description 2
[2] => Brand 2
[3] => Model 2
)
[2] => Array
(
[0] => Type 3
[1] => Description 3
[2] => Brand 3
[3] => Model 3
)
[3] => Array
(
[0] => Type 4
[1] => Description 4
[2] => Brand 4
[3] => Model 4
)
[4] => Array
(
[0] => Type 5
[1] => Description 5
[2] => Brand 5
[3] => Model 5
)
Je ne suis pas un expert PHP, aidez-moi :)
Je vous remercie.
4 réponses
Essayez ce code:
$type = array('Type 1','Type 2','Type 3',' Type 4','Type 5');
$desc = array('Description 1','Description 2','Description 3','Description 4','Description 5');
$brand = array('Brand 1','Brand 2','Brand 3','Brand 4','Brand 5');
$model = array('Model 1','Model 2','Model 3','Model 4','Model 5');
$result = array();
foreach($type as $key=>$val){ // Loop though one array
$val2 = $desc[$key];
$val3 = $brand[$key];
$val4 = $model[$key];
$result[$key] =array($val,$val2, $val3,$val4);
}
print_r($result);
Boucle simple à travers n'importe quel tableau, utilisez cette clé de tableau pour obtenir un autre tableau de données et créer un nouveau tableau
$type = array('Type 1','Type 2','Type 3',' Type 4','Type 5');
$desc = array('Description 1','Description 2','Description 3','Description 4','Description 5');
$brand = array('Brand 1','Brand 2','Brand 3','Brand 4','Brand 5');
$model = array('Model 1','Model 2','Model 3','Model 4','Model 5');
foreach($type as $key=>$type_val){ // Loop though one array
$desc_val = $desc[$key] ?? "";
$brand_val = $brand[$key] ?? "";
$model_val = $model[$key] ?? "";
$result[$key] = array($type_val,$desc_val,$brand_val,$model_val); // combine
}
print_r($result);
Remarque: ??
ne fonctionnera que pour php 7 + pour le changement de version plus ancienne $desc[$key] ?? ""
en isset($desc[$key]) ? $desc[$key] : ""
C'est un code parfait et testé:
<?php
$type = array('Type 1','Type 2','Type 3',' Type 4','Type 5');
$desc = array('Description 1','Description 2','Description 3','Description 4','Description 5');
$brand = array('Brand 1','Brand 2','Brand 3','Brand 4','Brand 5');
$model = array('Model 1','Model 2','Model 3','Model 4','Model 5');
$result = array();
foreach ($type as $id => $key) {
$result[$id] = array($key,$desc[$id],$brand[$id],$model[$id]);
}
echo "<pre>";print_r($result);
?>
array_map est vraiment tout ce dont vous avez besoin ici.
$type = array('Type 1','Type 2','Type 3',' Type 4','Type 5');
$desc = array('Description 1','Description 2','Description 3','Description 4','Description 5');
$brand = array('Brand 1','Brand 2','Brand 3','Brand 4','Brand 5');
$model = array('Model 1','Model 2','Model 3','Model 4','Model 5');
$res = array_map(function(...$val) {return $val;}, $type, $desc, $brand, $model);
print_r($res);
Questions connexes
De nouvelles questions
php
PHP est un langage de script largement utilisé, de haut niveau, dynamique, orienté objet et interprété, principalement conçu pour le développement Web côté serveur. Utilisé pour les questions sur le langage PHP.