ZCE
De l'esprit à la machine
L'approche Professo-Académique
Séances
Arrays
www.maroclance.com
www.MarocLance.com
Many modern programming languages — including
PHP — support two types of arrays:
www.MarocLance.com
Indexed arrays — These are arrays where each
element is referenced by a numeric index, usually
starting from zero. For example, the first element
has an index of 0, the second has an index of 1, and
so on
Associative arrays — This type of array is also
referred to as a hash or map. With associative arrays,
each element is referenced by a string index. For
example, you might create an array element
representing a customer's age and give it an index of
"age"
$authors = array( "Steinbeck", "Kafka",
"Tolkien", "Dickens" );
www.MarocLance.com
$myBook = array( "title" => "The Grapes of
Wrath",
www.MarocLance.com
"author" => "John Steinbeck",
"pubYear" => 1939 );
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
If in doubt, always initialize your array
variables when you first create them, even if
www.MarocLance.com
you're not creating any array elements at that
point. You can do this easily by using the
array() construct with an empty list:
$authors = array();
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
Arrays can get quite complex, as you see later in
the chapter, so often you'll find that you want to
www.MarocLance.com
inspect an array to see what it contains. You
can't just print an array with print() or echo(), like
you can with regular variables, because these
functions can work with only one value at a time.
However, PHP does give you a function called
print_r() that you can use to output the contents
of an array for debugging.
Using print_r() is easy — just pass it the array
you want to output:
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
Using foreach to Loop Through Values
www.MarocLance.com
TP sample indexed array
TP sample associative array
www.MarocLance.com
Using foreach to Loop Through Keys and
Values
www.MarocLance.com
TP sample indexed array
TP sample associative array
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
TP
www.MarocLance.com
TP
www.MarocLance.com
www.MarocLance.com
TP
www.MarocLance.com
Sorting Arrays
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
array_splice()
www.MarocLance.com
TP :: array_splice()
www.MarocLance.com
You can read this line as: "At the third
position (2), remove zero (0) elements, then
www.MarocLance.com
insert $arrayToAdd".
This code removes two elements from the
start of the array (position 0), then inserts the
www.MarocLance.com
contents of $arrayToAdd at position 0.
This code removes all the elements from the
second position in the array (position 1) to
www.MarocLance.com
the end of the array.
Notice how the "Milton" element has had its
original key ("authorName") replaced with a
www.MarocLance.com
numeric key (1).
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
http://www.php.net/manual/en/function.arra
y-replace.php
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
SPL is a standard PHP library. This is a
collection of interfaces and classes that are
www.MarocLance.com
used to solve standard problems. SPLs are
mainly designed to traverse aggregate
structures including arrays, database result
sets, xml trees, directory listings, or any list
at all.
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com
www.MarocLance.com