PHP Fonctions associées aux tableaux

15
16:28:21 16:28:21 Programmation Web 2012-2013 Programmation Web 2012-2013 1 PHP PHP Fonctions associées Fonctions associées aux tableaux aux tableaux Jérôme CUTRONA Jérôme CUTRONA [email protected] [email protected]

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 Presentation

Transcript of PHP Fonctions associées aux tableaux

Page 1: PHP Fonctions associées aux tableaux

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

Page 2: PHP Fonctions associées aux tableaux

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)) {{ ...... }}

Page 3: PHP Fonctions associées aux tableaux

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

Page 4: PHP Fonctions associées aux tableaux

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))

Page 5: PHP Fonctions associées aux tableaux

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

Page 6: PHP Fonctions associées aux tableaux

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))

Page 7: PHP Fonctions associées aux tableaux

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

Page 8: PHP Fonctions associées aux tableaux

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

Page 9: PHP Fonctions associées aux tableaux

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

Page 10: PHP Fonctions associées aux tableaux

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

Page 11: PHP Fonctions associées aux tableaux

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] )] )

Page 12: PHP Fonctions associées aux tableaux

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))

Page 13: PHP Fonctions associées aux tableaux

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

Page 14: PHP Fonctions associées aux tableaux

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))

Page 15: PHP Fonctions associées aux tableaux

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