Professional Documents
Culture Documents
Array Functions-Part Two
Array Functions-Part Two
print_r() rsort()
count() arsort()
sort() krsort()
asort() shuffle()
ksort() array_walk
print_r()
useful function to print the contents of an array
<pre>
<?php
$a = array(‘a’ => ‘apple’,
‘b’ => ‘banana’,
‘c’ => array(‘x’,’y’,’z’));
print_r($a);
?>
</pre>
print_r()
Sample Output:
Array
(
[a] => apple
[b] => banana
[c] => Array (
([0] => x
[1] => y
[2] => z
)
)
count()/sizeof()
Returns the number of elements in an array
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a); //result ==3
$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result= sizeof($b); //result == 3
sort()
sorts an array but destroy the keys.
$<?php
$customers = array(
'Matthew' => 'matthew@gmail.com',
'Mark' =>'mark@yahoo.com',
'Luke’ =>'luke@hotmail.com');
sort($customers);
while (list($key,$val) = each ($customers)){
echo "Element $key equals $val<br />";
}
?>
Output Program of sort()
asort($profile);
print_r($profile);
?>
ksort() function
<?php
$customers = array(
'Matthew' => 'matthew@gmail.com',
'Mark' =>'mark@yahoo.com',
'Luke’ =>'luke@hotmail.com');
ksort($customers);
while (list($key,$val) = each ($customers)){
echo "Element $key equals $val<br />";
}
?>
Sample Output:
Array
(
[0] => 2
[1] => 4
[2] => 3
[3] => 1
)
Randomizing and Reversing an Array
<?php
$rainbow=array('violet','indigo','blue','green',
'yellow','orange','red');
//randomize array
shuffle($rainbow);
print_r($rainbow);
echo "<br /><br />";
//reverse array
$arr=array_reverse($rainbow);
print_r($arr);
?>
Output of the Program
result of shuffle()
result of
array_reverse()
Searching Arrays
The in_array function looks through an array for a
specified value and returns true if found.
<?php
$cities=array('London','Paris','Barcel
ona','Lisbon','Zurich');
//search array for value
echo in_array('Barcelona',$cities);
?>
result is : 1
Searching arrays using
array_key_exists()
<?php
$cities=array(
"United Kingdom" => "London",
"United States" => "Washington",
"France" => "Paris",
"India" => "Delhi");
//search array for key
echo
array_key_exists('India',$cities);
?>
result is : 1
Extracting Array Segments
Extracts a segment of an array.
<?php
$rainbow=array('violet','indigo','blue'
,'green', 'yellow','orange','red');
//extract 3 central values
//output ('blue', 'green', 'yellow')
$arr= array_slice($rainbow,2,3);
print_r($arr);
?>
Output:
('blue', 'green', 'yellow')
Adding and Removing an Array
Element
$movies=array("The Lion King", "Cars","A Bug's Life");
//remove element from beginning of an array
array_shift($movies);
//print array
$HospitalDepts = array(
"Anesthesia", // first element (0)
"Molecular Biology", // second element (1)
"Neurology", // third element (2)
"Pediatrics"); // fourth element (3)
array_splice($HospitalDepts, 3, 0, "Ophthalmology");
<?php
$duplicates=array('a','b','a','c','e','d','e'
);
//remove duplicates
//output:(a,b,c,e,d)
$uniques=array_unique($duplicates);
print_r($uniques);
?>
Removing Duplicate Elements
(continued)
$TopSellers = array(
"Ford F-Series", "Chevrolet Silverado", "Toyota Camry",
"Honda Accord", "Toyota Corolla", "Ford F-Series", "Honda
Civic",
"Honda CR-V", "Honda Accord", "Nissan Altima", "Toyota
Camry",
"Chevrolet Impala", "Dodge Ram", "Honda CR-V");
echo "<p>The 2008 top selling vehicles are:</p><p>";
$TopSellers = array_unique($TopSellers);
$TopSellers = array_values($TopSellers);
for ($i=0; $i<count($ TopSellers); ++$i) {
echo "{$TopSellers[$i]}<br />";
}
echo "</p>";