Supprimer élément tableau php
Résolu
gismin
Messages postés
22
Date d'inscription
Statut
Membre
Dernière intervention
-
gismin Messages postés 22 Date d'inscription Statut Membre Dernière intervention -
gismin Messages postés 22 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
J'ai essayé de plusieurs façon de supprimer un élément d'un tableau et ... j'y comprend rien.
<?php echo 'ESSAIS <br><br>'; $pop2 = 'a2'; $tab[] = array('a1','a2','a3'); print_r($tab); echo '<br><br> ----------1----------<br> '; unset($tab[array_search($pop2,$tab)]); print_r($tab); echo '<br><br>-----------2---------<br> '; $tab = array_diff( $tab, $pop2); print_r($tab); echo '<br><br>---------3-----------<br> '; $pop2 = array_search('pop2', $tab); $tab = array_splice($tab, $pop2); print_r($tab); ?>
Résultat:
ESSAIS
Array ( [0] => Array ( [0] => a1 [1] => a2 [2] => a3 ) )
----------1----------
Array ( )
-----------2---------
Fatal error: Uncaught TypeError: array_diff(): Argument #2 must be of type array, string given in C:\xampp\htdocs\Desortie-B1\pub\essaiTableau.php:14 Stack trace: #0 C:\xampp\htdocs\Desortie-B1\pub\essaiTableau.php(14): array_diff(Array, 'a2') #1 {main} thrown in C:\xampp\htdocs\Desortie-B1\pub\essaiTableau.php on line 14
Windows / Chrome 112.0.0.0
Rien ne marche correctement.
A voir également:
- Supprimer élément tableau php
- Supprimer rond bleu whatsapp - Guide
- Tableau word - Guide
- Tableau ascii - Guide
- Supprimer page word - Guide
- Trier un tableau excel - Guide
3 réponses
heu ... j'ai vu une erreur:
$tab[] = array('a1','a2','a3'); // incorrect
$tab = array('a1','a2','a3'); // correct
J'ai une réponse correcte pour ---1--- :
unset($tab[array_search($pop2,$tab)]);
mais pas pour les 2 suivantes.