PHP Arrays: Dr. Charles Severance
PHP Arrays: Dr. Charles Severance
http://www.wa4e.com/code/arrays
http://www.wa4e.com/code/arrays.zip
PHP Arrays Rock!
• Better than Python Dictionaries
• Better than Java Hash Maps
• PHP Arrays have all the benefits of Python Dictionaries but
they can also maintain the order of the items in the array
http://en.wikipedia.org/wiki/Associative_array
Associative Arrays
Can be key => value or simply indexed by numbers
Ignore two-dimensional arrays for now...
http://en.wikipedia.org/wiki/Associative_array
Integer Indices
<?php
$stuff = array("Hi", "There");
echo $stuff[1], "\n";
?>
There
Key / Value
<?php
$stuff = array("name" => "Chuck",
"course" => "WA4E");
echo $stuff["course"], "\n";
?>
WA4E
Dumping an Array
The function print_r() shows PHP data - it is good for debugging.
<?php
$stuff = array("name" => "Chuck",
"course" => "WA4E");
echo("<pre>\n");
print_r($something);
echo("\n</pre>\n"); Array(
?> [name] => Chuck
[course] => WA4E
)
var_dump vs. print_r
<?php
$stuff = array("name" => "Chuck",
"course" => "SI664");
var_dump($stuff);
?>
array(2) {
["name"]=>
string(5) "Chuck"
["course"]=>
string(5) "SI664"
}
var_dump() and false
<?php
$thing = FALSE;
echo("One\n");
print_r($thing);
echo("Two\n"); One
var_dump($thing); Two
?> bool(false)
http://stackoverflow.com/questions/3406171/php-var-dump-vs-print-r
Building Up an Array
You can allocate a new item in the array and append a value
at the same time using empty square braces [ ] on the right
hand side of an assignment statement.
Key=0 Val=Chuck
Key=1 Val=SI664
Counted Loop Through an Array
<?php
$stuff = array("Chuck","SI664");
for($i=0; $i < count($stuff); $i++) {
echo "I=",$i," Val=",$stuff[$i],"\n";
}
?>
I=0 Val=Chuck
I=1 Val=SI664
Arrays of $products = array(
'paper' => array(
'copier' => "Copier & Multipurpose",
echo $products["pens"]["marker"];
Markers
Array Functions
Array Functions
array_key_exists($key, $ar) - Returns TRUE if key is set in the array
isset($ar['key']) - Returns TRUE if key is set in the array
count($ar) - How many elements in an array
is_array($ar) - Returns TRUE if a variable is an array
sort($ar) - Sorts the array values (loses key)
ksort($ar) - Sorts the array by key
asort($ar) - Sorts array by value, keeping key association
shuffle($ar) - Shuffles the array into random order
$za = array();
Course exists
$za["name"] = "Chuck"; name is set
$za["course"] = "WA4E"; addr is not set
if (array_key_exists('course',$za) ) {
echo("Course exists\n");
} else {
echo("Course does not exist\n");
}
Parse
Response $_GET
PHP php
code
ind.php
JavaScrip
t
RRC/HTTP SQL
http://www.wa4e.com/code/arrays/get-01.php?x=2&y=4
Summary
This is a sprint through some of the unique
language features of PHP Arrays.
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance Continue new Contributors and Translators here
(www.dr-chuck.com) as part of www.wa4e.com and made
available under a Creative Commons Attribution 4.0 License.
Please maintain this last slide in all copies of the document to
comply with the attribution requirements of the license. If you
make a change, feel free to add your name and organization
to the list of contributors on this page as you republish the
materials.