2.2 PHP Arrays - Functions
2.2 PHP Arrays - Functions
WEB APPLICATIONS
PHP Arrays
Introduction to Dynamic Web Content FOR EVERYBODY
FOR EVERYBODY
PHP Arrays
Dr. Charles Severance
www.wa4e.com
http://www.wa4e.com/code/arrays
http://www.wa4e.com/code/arrays.zip
WEB APPLICATIONS
WEB APPLICATIONS
PHP Arrays
Introduction to Dynamic Web Content FOR EVERYBODY
FOR EVERYBODY
Array Functions
WEB APPLICATIONS
PHP Arrays FOR EVERYBODY
WEB APPLICATIONS
PHP Arrays FOR EVERYBODY
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
WEB APPLICATIONS
PHP Arrays FOR EVERYBODY
$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");
}
<?php
$za = array(); Null
Coalesce
$za["name"] = "Chuck";
$za["course"] = "WA4E";
Count: 2
$za = array(); $za Is an array
$za["name"] = "Chuck";
$za["course"] = "WA4E";
print "Count: " . count($za) . "\n";
if ( is_array($za) ) {
echo '$za Is an array' . "\n";
} else {
echo '$za Is not an array' . "\n";
}
WEB APPLICATIONS
PHP Arrays FOR EVERYBODY
Array(
[name] => Chuck
$za = array(); [course] => WA4E
[topic] => PHP
$za["name"] = "Chuck";
)
$za["course"] = "WA4E";
$za["topic"] = "PHP"; Array(
print_r($za); [course] => WA4E
ksort($za); [name] => Chuck
print_r($za); [topic] => PHP
asort($za); )
print_r($za);
Array(
[name] => Chuck
[topic] => PHP
[course] => WA4E
)
WEB APPLICATIONS
PHP Arrays FOR EVERYBODY
Exploding Arrays
$inp = "This is a sentence with seven words";
$temp = explode(' ', $inp);
print_r($temp); Array(
[0] => This
[1] => is
[2] => a
[3] => sentence
[4] => with
[5] => seven
[6] => words
)
WEB APPLICATIONS
PHP Arrays FOR EVERYBODY
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.