KEMBAR78
ZCE (Array) PDF | PDF | Array Data Type | Array Data Structure
0% found this document useful (0 votes)
68 views81 pages

ZCE (Array) PDF

The document discusses PHP arrays, including indexed arrays and associative arrays. It covers initializing arrays, accessing array elements, looping through arrays using foreach, sorting arrays, modifying arrays using functions like array_splice() and array_replace(), and the PHP Standard Library (SPL) which contains classes and interfaces for working with aggregate data structures like arrays.

Uploaded by

Abdelahad Satour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views81 pages

ZCE (Array) PDF

The document discusses PHP arrays, including indexed arrays and associative arrays. It covers initializing arrays, accessing array elements, looping through arrays using foreach, sorting arrays, modifying arrays using functions like array_splice() and array_replace(), and the PHP Standard Library (SPL) which contains classes and interfaces for working with aggregate data structures like arrays.

Uploaded by

Abdelahad Satour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

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

You might also like