Changer les clés d'un tableau multidimensionnel

Suivre ce topic
Ce topic est suivi par : Azee
Azee

Azee Le 17 mars 2017 à 11:14 (Édité le 25 janvier 2019 à 17:53)

Bonjour, 

Bon, je m'en reviens vers vous après 3 heures sur le problème, je n'en peux plus...

J'ai un array que voici : 
D:\wamp64\www\index.php:9:
array (size=4)
  0 => 
    array (size=5)
      'id' => string '3' (length=1)
      'groupname' => string 'test' (length=4)
      'cinema_group' => string '2' (length=1)
      'cinema_name' => string 'test_1' (length=6)
      'cinema_location' => string 'test_location1' (length=14)
  1 => 
    array (size=5)
      'id' => string '4' (length=1)
      'groupname' => string 'test' (length=4)
      'cinema_group' => string '2' (length=1)
      'cinema_name' => string 'test_2' (length=6)
      'cinema_location' => string 'test_location2' (length=14)
  2 => 
    array (size=5)
      'id' => string '5' (length=1)
      'groupname' => string 'test' (length=4)
      'cinema_group' => string '2' (length=1)
      'cinema_name' => string 'test3' (length=5)
      'cinema_location' => string 'test_location3' (length=14)
  3 => 
    array (size=5)
      'id' => string '6' (length=1)
      'groupname' => string 'test' (length=4)
      'cinema_group' => string '2' (length=1)
      'cinema_name' => string 'test4' (length=5)
      'cinema_location' => string 'test_location4' (length=14)
Je veux juste modifier les clés 0, 1, 2 et 3 par la valeur de l'id, pour avoir ca :
D:\wamp64\www\index.php:9:
array (size=4)
  3 => 
    ...
  4 => 
    ...
  5 => 
    ...
  6 => 
    ...

Aucun moyen d'y arriver, j'ai passé 3 heures en essayant avec les fonctions php sur les tableaux, des foreach...

Si vous avez une solution, je suis preneur !
Boris95098

Boris95098 Le 17 mars 2017 à 20:11

Yo ! 

Pourquoi veux-tu modifier les clés ? Dans quel cas utilises-tu ce code ?
Azee

Azee Le 17 mars 2017 à 21:45

Je veux modifier les clés pour retrouver mes tableaux par l'id.
Pour afficher des informations clients
Zbuu

Zbuu Le 17 mars 2017 à 22:09

Salut 

Tu peux stocker ton tableau dans une variable et faire un foreach 

foreach ($array as $k => $v) {
   $new[$v['id']]['groupname'] = $v['groupname'];  
   $new[$v['id']]['cinema_group'] = $v['cinema_group'];  
   $new[$v['id']]['cinema_name'] = $v['cinema_name'];  
   $new[$v['id']]['cinema_location'] = $v['cinema_location'];  
}
fait gaffe si tu c/c j'ai fais ça via mon tel alors il y a peut être des erreurs 
Vous devez être connecté pour poster une réponse. Se connecter ou Créer un compte