php fonctions associées aux tableaux
DESCRIPTION
PHP Fonctions associées aux tableaux. Jérôme CUTRONA [email protected]. Rappels. Tableaux associatifs fait correspondre des valeurs à des clés clés : entier ou chaîne de caractères valeurs : ce que l'on souhaite, pas forcément homogène Syntaxe - PowerPoint PPT PresentationTRANSCRIPT
22:32:3222:32:32 Programmation Web 2012-2013Programmation Web 2012-2013 11
PHPPHPFonctions associées aux Fonctions associées aux
tableauxtableauxJérôme CUTRONAJérôme CUTRONA
[email protected]@univ-reims.fr
2222:32:3222:32:32 Programmation Web 2012-2013Programmation Web 2012-2013
RappelsRappels
Tableaux Tableaux associatifsassociatifs fait fait correspondrecorrespondre des des valeursvaleurs à des à des clésclés
clésclés : entier ou chaîne de caractères : entier ou chaîne de caractères
valeursvaleurs : ce que l'on souhaite, pas forcément homogène : ce que l'on souhaite, pas forcément homogène
SyntaxeSyntaxe $$tt == arrayarray((11, ', 'aa''));; $$tt == arrayarray((''bb''=>=>11, , 22=>=>''aa''));;
$$tt[][] == ' 'bb'';; $$tt[[''zz'']] == ' 'ss'';;
ParcoursParcours foreachforeach (($$tt asas $$valval)) {{ ...... }}
foreachforeach (($$tt asas $$clecle =>=> $$valval)) {{ ...... }}
3322:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux vus comme des piles/filesTableaux vus comme des piles/files
EmpilerEmpiler
int int array_pusharray_push ( array & ( array &tabtab, mixed , mixed varvar [, mixed [, mixed ......] )] )
considère considère tabtab comme une pile, et empile les variables comme une pile, et empile les variables varvar, , ...... à la fin de à la fin de tabtab
retourne le nouveau nombre d'éléments du tableau. retourne le nouveau nombre d'éléments du tableau.
DépilerDépiler
mixed mixed array_poparray_pop ( array & ( array &tabtab ) )
dépile et retourne le dernier élément du tableau dépile et retourne le dernier élément du tableau tabtab
00 11 22 33 44 pushpushpoppop
4422:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux vus comme des piles/filesTableaux vus comme des piles/files
$$stackstack == arrayarray ((""orangeorange", "", "bananebanane""));;
array_pusharray_push (($$stackstack, ", "pommepomme", "", "framboiseframboise""));;
print_rprint_r(($$stackstack));;
$$stackstack == arrayarray((""orangeorange", "", "bananebanane",",
""pommepomme", "", "framboiseframboise""));;
$$fruitfruit == array_poparray_pop(($$stackstack));;
print_rprint_r(($$stackstack));;
echoecho $$fruitfruit..""\n\n"";;
ArrayArray(( [0] => orange [0] => orange [1] => banane [1] => banane [2] => pomme [2] => pomme))framboiseframboise
ArrayArray(( [0] => orange [0] => orange [1] => banane [1] => banane [2] => pomme [2] => pomme [3] => framboise [3] => framboise))
5522:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux vus comme des piles/filesTableaux vus comme des piles/files
EnfilerEnfiler
int int array_unshiftarray_unshift ( array & ( array &tabtab, mixed , mixed varvar [, mixed [, mixed ......] )] )
considère considère tabtab comme une file, et enfile les variables comme une file, et enfile les variables varvar, , ...... au début de au début de tabtab
retourne le nouveau nombre d'éléments du tableau. retourne le nouveau nombre d'éléments du tableau.
DéfilerDéfiler
mixed mixed array_shiftarray_shift ( array & ( array &tabtab ) )
défile et retourne le premier élément du tableau défile et retourne le premier élément du tableau tabtab
00 00 11 22 33 unshiftunshiftshiftshift11 22 33 44
6622:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tableaux vus comme des piles/filesTableaux vus comme des piles/files
$$queuequeue == arrayarray ((""orangeorange", "", "bananebanane""));;
array_unshiftarray_unshift (($$queuequeue,,
""pommepomme", "", "framboiseframboise""));;
print_rprint_r(($$queuequeue));;
$$stackstack == arrayarray((""orangeorange", "", "bananebanane",",
""pommepomme", "", "framboiseframboise""));;
$$fruitfruit == array_shiftarray_shift(($$stackstack));;
print_rprint_r(($$stackstack));;
echoecho $$fruitfruit..""\n\n"";;
ArrayArray(( [0] => banane [0] => banane [1] => pomme [1] => pomme [2] => framboise [2] => framboise))orangeorangeArrayArray(( [0] => pomme [0] => pomme [1] => framboise [1] => framboise [2] => orange [2] => orange [3] => banane [3] => banane))
7722:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tris sur les tableauxTris sur les tableaux
$$fruitsfruits == arrayarray((""lemonlemon", "", "orangeorange",",
""bananabanana", "", "appleapple""));;
bool bool sortsort ( array & ( array &tabtab [, int [, int sort_flagssort_flags] )] )sortsort(($$fruitsfruits));;
foreachforeach (($$fruitsfruits asas $$keykey =>=> $$valval)) {{
echoecho " "fruits[fruits["".$.$keykey..""] = ] = "".$.$valval..""\n\n"";; }}
bool bool rsortrsort ( array & ( array &tabtab [, int [, int sort_flagssort_flags] )] )rsortrsort(($$fruitsfruits));;
foreachforeach (($$fruitsfruits asas $$keykey =>=> $$valval)) {{
echoecho " "fruits[fruits["".$.$keykey..""] = ] = "".$.$valval..""\n\n"";; }}
fruits[fruits[00] = orange] = orangefruits[fruits[11] = lemon] = lemonfruits[fruits[22] = banana] = bananafruits[fruits[33] = apple] = apple
fruits[fruits[00] = apple] = applefruits[fruits[11] = banana] = bananafruits[fruits[22] = lemon] = lemonfruits[fruits[33] = orange] = orange
8822:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tris sur les tableauxTris sur les tableaux
$$fruitsfruits == arrayarray((""lemonlemon", "", "orangeorange",",
""bananabanana", "", "appleapple""));;
bool bool asortasort ( array & ( array &tabtab [, int [, int sort_flagssort_flags] )] )asortasort(($$fruitsfruits));;
foreachforeach (($$fruitsfruits asas $$keykey =>=> $$valval)) {{
echoecho " "fruits[fruits["".$.$keykey..""] = ] = "".$.$valval..""\n\n"";; }}
bool bool arsortarsort ( array & ( array &tabtab [, int [, int sort_flagssort_flags] )] )arsortarsort(($$fruitsfruits));;
foreachforeach (($$fruitsfruits asas $$keykey =>=> $$valval)) {{
echoecho " "fruits[fruits["".$.$keykey..""] = ] = "".$.$valval..""\n\n"";; }}
fruits[fruits[11] = orange] = orangefruits[fruits[00] = lemon] = lemonfruits[fruits[22] = banana] = bananafruits[fruits[33] = apple] = apple
fruits[fruits[33] = apple] = applefruits[fruits[22] = banana] = bananafruits[fruits[00] = lemon] = lemonfruits[fruits[11] = orange] = orange
9922:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Tris sur les tableauxTris sur les tableaux
$$fruitsfruits= = arrayarray((""dd""=>=>""lemonlemon", "", "aa""=>=>""orangeorange",",
""bb""=>=>""bananabanana", "", "cc""=>=>""appleapple""));;
bool bool ksortksort ( array & ( array &tabtab [, int [, int sort_flagssort_flags] )] )ksortksort(($$fruitsfruits));;
foreachforeach (($$fruitsfruits asas $$keykey =>=> $$valval)) {{
echoecho " "fruits[fruits["".$.$keykey..""] = ] = "".$.$valval..""\n\n"";; }}
bool bool krsortkrsort ( array & ( array &tabtab [, int [, int sort_flagssort_flags] )] )krsortkrsort(($$fruitsfruits));;
foreachforeach (($$fruitsfruits asas $$keykey =>=> $$valval)) {{
echoecho " "fruits[fruits["".$.$keykey..""] = ] = "".$.$valval..""\n\n"";; }}
fruits[fruits[dd] = lemon] = lemonfruits[fruits[cc] = apple] = applefruits[fruits[bb] = banana] = bananafruits[fruits[aa] = orange] = orange
fruits[fruits[aa] = orange] = orangefruits[fruits[bb] = banana] = bananafruits[fruits[cc] = apple] = applefruits[fruits[dd] = lemon] = lemon
101022:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Mélanges aléatoires sur les tableauxMélanges aléatoires sur les tableaux
$$numbersnumbers == rangerange((11, , 1010));;
bool bool shuffleshuffle ( array & ( array &tabtab ) )shuffleshuffle(($$numbersnumbers));;
foreachforeach (($$numbersnumbers asas $$numbernumber)) {{
echoecho " "$$numbernumber "";;
}}
9 7 6 5 2 4 1 3 10 89 7 6 5 2 4 1 3 10 8
111122:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Fonctions utiles sur les tableauxFonctions utiles sur les tableaux
int int countcount ( mixed ( mixed varvar [, int [, int modemode] )] ) bool bool array_key_existsarray_key_exists ( mixed ( mixed keykey,,
array array searchsearch ) ) array array array_keysarray_keys ( array ( array inputinput
[, mixed [, mixed search_valuesearch_value [, bool [, bool strictstrict]] )]] )
array array array_valuesarray_values ( array ( array inputinput ) ) mixed mixed array_searcharray_search ( mixed ( mixed needleneedle,,
array array haystackhaystack [, bool [, bool strictstrict] )] ) bool bool in_arrayin_array ( mixed ( mixed needleneedle,,
array array haystackhaystack [, bool [, bool strictstrict] )] )
121222:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Implosion / explosionImplosion / explosion
array array explodeexplode ( string ( string delimiterdelimiter, string , string stringstring [, int [, int limitlimit] )] )
$$pizzapizza ==
""piece1 piece2 piece3 piece4 piece5 piece6piece1 piece2 piece3 piece4 piece5 piece6"";;
$$piecespieces == explodeexplode(("" ", ", $$pizzapizza));;
print_rprint_r(($$piecespieces));;
$$datadata == " "foo:*:1023:1000::/home/foo:/bin/shfoo:*:1023:1000::/home/foo:/bin/sh"";;
listlist(($$useruser, , $$passpass, , $$uiduid, , $$gidgid, , $$gecosgecos,,
$$homehome, , $$shellshell)) == explodeexplode((""::", ", $$datadata));;
echoecho $$useruser..""\n\n"";; echoecho $$passpass..""\n\n"";;
foofoo**10231023
Array (Array ( [0] => piece1[0] => piece1 [1] => piece2[1] => piece2 [2] => piece3[2] => piece3 [3] => piece4[3] => piece4 [4] => piece5 [4] => piece5 [5] => piece6[5] => piece6))
131322:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Implosion / explosionImplosion / explosion
string string implodeimplode ( string ( string glueglue, array , array piecespieces ) )
$$arrayarray ==
arrayarray((''lastnamelastname', '', 'emailemail', '', 'phonephone''));;
$$comma_separatedcomma_separated == implodeimplode(("",,", ", $$arrayarray));;
echoecho $$comma_separatedcomma_separated..""\n\n"";;
lastname,email,phonelastname,email,phone
Récupération des clés / valeursRécupération des clés / valeurs
arrayarray array_keysarray_keys ( ( arrayarray inputinput ) )
$$arrayarray == arrayarray((00 =>=> 100100, ", "colorcolor" " =>=> " "redred""));;print_rprint_r((array_keysarray_keys(($$arrayarray))));;
arrayarray array_valuesarray_values ( ( arrayarray inputinput ) )
$ $arrayarray == arrayarray((00 =>=> 100100, ", "colorcolor" " =>=> " "redred""));;print_rprint_r((array_valuesarray_values(($$arrayarray))));;
141422:32:3322:32:33 Programmation Web 2012-2013Programmation Web 2012-2013
Array (Array ( [0] => 0[0] => 0 [1] => color[1] => color))
Array (Array ( [0] => 100[0] => 100 [1] => red[1] => red))
Toujours plus de fonctionnalitésToujours plus de fonctionnalités
Consulter la documentation officielleConsulter la documentation officielle
http://fr.php.net/manual/fr/book.array.phphttp://fr.php.net/manual/fr/book.array.php
151522:32:3422:32:34 Programmation Web 2012-2013Programmation Web 2012-2013