1
Single Dimenssional Numeric array
Example 1:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$items = Array("Orange","Apple") ;
foreach($items as $value)
print $value . "<br>" ;
?>
</body>
</html>
Output
Orange
Apple
Example 2:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$items = Array("Orange","Apple") ;
print_r($items);
?>
</body>
</html>
2
Output
Array ( [0] => Orange [1] => Apple )
Multidimenssional numeric array
Example:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$items = Array( "item0" => Array ("Orange", "Apple") ,
"item1" => Array ("Cat", "Dog" )
);
print_r($items);
?>
</body>
</html>
Output
Array ( [item0] => Array ( [0] => Orange [1] => Apple ) [item1] => Array ( [0] => Cat
[1] => Dog ) )
Associative Array
Example:
html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
3
$employeeAges["Dipu"] = "27" ;
$employeeAges["Ramu"] = "26" ;
$employeeAges["Manu"] = "25" ;
foreach($employeeAges As $key => $value) {
echo "Name: " . $key . ", Age: " . $value . "<br>" ;
}
?>
</body>
</html>
Output
Name: Dipu, Age: 27
Name: Ramu, Age: 26
Name: Manu, Age: 25
Example 2:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$employeeAges = Array("Dipu" => "27",
"Ramu" => "26",
"Manu" => "25" ) ;
print_r($employeeAges);
?>
</body>
</html>
Output
Array ( [Dipu] => 27 [Ramu] => 26 [Manu] => 25 )
4
Using print_r to print contents of array
Example 1:
<html>
<head>
<title>Using print_r </title>
</head>
<body>
<?php
$animals = Array("Cat","Dog");
print_r($animals);
?>
</body>
</html>
Output
Array ( [0] => Cat [1] => Dog )
Example 2:
<html>
<head>
<title>Using print_r </title>
</head>
<body>
<?php
$animals = Array("a"=>"Cat","b"=>"Dog");
print_r($animals);
?>
</body>
</html>
Output:
Array ( [a] => Cat [b] => Dog )
5
Count the elements in Array
Example:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$employeeAges = Array("Dipu" => "27",
"Ramu" => "26",
"Manu" => "25" ) ;
print "Count is " . count($employeeAges) ;
?>
</body>
</html>
Output
Count is 3
Insert into an Array (array_unshift())
Example 1:
<html>
<head>
<title>Insert into an array</title>
</head>
<body>
<?php
$animals = Array("a"=>"Cat","b"=>"Dog");
array_unshift($animals, "Horse", "Cow");
print_r($animals);
?>
6
</body>
</html>
Output:
Array ( [0] => Horse [1] => Cow [a] => Cat [b] => Dog )
Append elements to an array [array_push]
Example 1:
<html>
<head>
<title>Append elements to the array</title>
</head>
<body>
<?php
$numbers = Array(10,20,30);
print "Before appending..<br>" ;
print_r($numbers);
array_push($numbers,40,50);
print "<br>After appending.. <br>" ;
print_r($numbers) ;
?>
</body>
</html>
Output
Before appending..
Array ( [0] => 10 [1] => 20 [2] => 30 )
After appending..
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )
Delete elements from an Array [array_pop]
Example:
<html>
<head>
7
<title>Append elements to the array</title>
</head>
<body>
<?php
$numbers = Array(10,20,30,40);
print "Before poping..<br>" ;
print_r($numbers);
$element = array_pop($numbers);
print "<br>Element popped : " . $element ;
print "<br>After poping..<br>" ;
print_r($numbers) ;
?>
</body>
</html>
Output:
Before poping..
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 )
Element popped : 40
After poping..
Array ( [0] => 10 [1] => 20 [2] => 30 )
Sort a numeric array
Example 1:
<html>
<head>
<title>Sort ordinary array</title>
</head>
<body>
<?php
$numbers = Array(30,10,20,40);
print "Before sorting..<br>" ;
print_r($numbers);
print "<br>After Sorting..<br>" ;
sort($numbers);
print_r($numbers) ;
8
?>
</body>
</html>
Output:
Before sorting..
Array ( [0] => 30 [1] => 10 [2] => 20 [3] => 40 )
After Sorting..
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 )
Sort an array in reverse order
Example:
<html>
<head>
<title>Sort ordinary array</title>
</head>
<body>
<?php
$numbers = Array(30,10,20,40);
print "Before sorting..<br>" ;
print_r($numbers);
print "<br>After Sorting..<br>" ;
rsort($numbers);
print_r($numbers) ;
?>
</body>
</html>
Output:
Before sorting..
Array ( [0] => 30 [1] => 10 [2] => 20 [3] => 40 )
After Sorting..
Array ( [0] => 40 [1] => 30 [2] => 20 [3] => 10 )
9
Sorting an associative array
Example:
<html>
<head>
<title>Sort associative array</title>
</head>
<body>
<?php
$fruits = Array("o"=>Orange,"a"=>"Apple","g"=>"Grapes");
print "Before sorting..<br>" ;
print_r($fruits);
print "<br>After Sorting..<br>" ;
asort($fruits);
print_r($fruits) ;
?>
</body>
</html>
Output:
Before sorting..
Array ( [o] => Orange [a] => Apple [g] => Grapes )
After Sorting..
Array ( [a] => Apple [g] => Grapes [o] => Orange )
Reverse sort an associative array
Example:
<html>
<head>
<title>Sort associative array</title>
</head>
<body>
<?php
$fruits = Array("o"=>Orange,"a"=>"Apple","g"=>"Grapes");
print "Before sorting..<br>" ;
print_r($fruits);
10
print "<br>After Sorting..<br>" ;
arsort($fruits);
print_r($fruits) ;
?>
</body>
</html>
Output:
Before sorting..
Array ( [o] => Orange [a] => Apple [g] => Grapes )
After Sorting..
Array ( [o] => Orange [g] => Grapes [a] => Apple )