Array & Its Functions in PHP – Part 1
A variable which can hold multiple values (more than one) at one time is known as arrays. For Example if you want to store list of cycles in one variable, you could do that in arrays like this
$cycle[0] = “ATLAS”
$cycle[1] = “HERO”
$cycle[2] = ”BSA”
$cycle[3] = ”AVON”
There are 3 types of array in PHP
- Associative array
- Index array
- Multidimensional array
PHP Associative Arrays
Arrays which used name key to assign them are known as associative arrays.
We can create associative array in two ways:
$age = array("Ramesh"=>"25", "Suresh"=>"45", "Rajesh"=>"75");
or:
$age[‘Ramesh’] = "25";
$age[‘Suresh’] = "45";
$age[‘Rajesh’] = "75";
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Ramesh"=>"25", "Suresh"=>"48", "Rajesh"=>"75");
echo "Suresh is " . $age[’48’] . " years old.";
?>
</body>
</html>
Result : Suresh is 45 years old.
PHP Indexed Arrays
Indexed Arrays can be used in two ways:
Automatically assign: The index can be assigned automatically (index always begins with 0), like this:
$cycle = array("Atlas", "Hero", "BSA");
Manually assign: The index can be assigned manually:
$cycle[0] = "Atlas";
$cycle[1] = "BSA";
$cycle[2] = "Hero";
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$cycle = array("Hero", "BSA", "Atlas");
echo "I like " . $cycle[0] . “, ” . $cycle[1] . ” and ” . $cycle[2] . ".";
?>
</body>
</html>
Result : I like Hero, BSA and Alas.
PHP – Multidimensional Arrays
An array which contain more than one array is known as multidimensional array.
PHP understands multidimensional arrays that are second, third, fourth, fifth, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
Name | Stock | Sold |
Hero | 5 | 2 |
BSA | 15 | 13 |
Atlas | 22 | 18 |
Hercules | 17 | 15 |
We can store the data from the table above in a two-dimensional array, like this:
$cycle = array
(
array("Hero",5,2),
array("BSA",15,13),
array("Atlas",22,18),
array("Hercules",17,15)
);
Now the two-dimensional $cycle array contains four arrays, and it has two indices: row and column.
To get access to the elements of the $cycle array we must point to the two indices (row and column):
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$cycle = array
(
array("Hero",5,2),
array("BSA",15,13),
array("Atlas",22,18),
array("Hercules",17,15)
);
echo $cycle[0][0].”: In stock: “.$cycle[0][1].”, sold: “.$cycle[0][2].".<br>";
echo $cycle[1][0].”: In stock: “.$cycle[1][1].”, sold: “.$cycle[1][2].".<br>";
echo $cycle[2][0].”: In stock: “.$cycle[2][1].”, sold: “.$cycle[2][2].".<br>";
echo $cycle[3][0].”: In stock: “.$cycle[3][1].”, sold: “.$cycle[3][2].".<br>";
?>
</body>
</html>
Result : Hero: In stock: 5 sold: 2.
BSA: In stock: 15, sold: 13.
Atlas: In stock: 2, sold: 18.
Hercules: In stock: 17, sold: 15.
Array Functions
array() | form an array |
Example : $institute = "cpd technologies";
array_change_key_case() | Convert all keys of an array to uppercase or lowercase |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$age=array("html"=>"74","php"=>"49","java"=>"27");
print_r(array_change_key_case($age,CASE_UPPER));
?>
</body>
</html>
Result : Array ( [HTML] => 74 [PHP] => 49 [JAVA] => 27 )
array_chunk() | breaks an array into pieces of arrays(chunks) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$courses=array("Java","C","Php",".net","android","C++");
print_r(array_chunk($courses,2));
?>
</body>
</html>
Results: Array ( [0] => Array ( [0] => Java [1] => C ) [1] => Array ( [0] => Php [1] => .net) [2] => Array ( [0] => android [1] => C++ ) )
array_column() | Returns the values from one column in the input array |
Example :
<?php
// An array that represents a record set returned from a database
$a = array(
array(
'id' => 5698,
'first_name' => cpd,
'last_name' => india',
),
array(
'id' => 4767,
'first_name' => 'cpd',
'last_name' => 'technology',
),
array(
'id' => 3809,
'first_name' => 'ignou',
'last_name' => cpd',
)
);
$last_names = array_column($a, 'last_name');
print_r($last_names);
?>
Result: Array
(
[0] => cpd
[1] => cpd
[2] => cpd
)
array_combine() | makes an array by using the item from one "keys" array and one "values" array |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$fname=array("CPD","India","Technology");
$age=array("46","59","73");
$c=array_combine($fname,$age);
print_r($c);
?>
</body>
</html>
Result : Array ( [CPD] => 46 [India] => 59 [Technology] => 73 )
array_count_values() | calculate all the values of an array |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("CPD","Technologies","India","CPD","Technologies");
print_r(array_count_values($a));
?>
</body>
</html>
Results : Array ( [CPD] => 2 [India] => 1 [Technologies] => 2 )
array_diff() | Compare arrays, and returns the differences (compare values only) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"c language","b"=>"android","c"=>"php","d"=>"java");
$a2=array("e"=>" c language ","f"=>" android ","g"=>"php");
$result=array_diff($a1,$a2);
print_r($result);
?>
</body>
</html>
Results : Array ( [d] => java )
array_diff_assoc() | Compare arrays, and returns the differences (compare keys and values) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"c language","b"=>" android ","c"=>"php","d"=>"java");
$a2=array("e"=>" c language ","f"=>" android ","g"=>"php");
$result=array_diff($a1,$a2);
print_r($result);
?>
</body>
</html>
Results : Array ( [d] => java )
array_diff_key() | Compare arrays, and returns the differences (compare keys only) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>" c language ","b"=>"java","c"=>" php ");
$a2=array("a"=>" .net ","c"=>"android","d"=>"cloud computing");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
</body>
</html>
Result : Array ( [b] => java )
array_diff_uassoc() | Match arrays, and revert the differences (compare values and keys, using a user-defined key comparison function) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"java","b"=>".net","c"=>"php");
$a2=array("d"=>"java","b"=>".net","e"=>"php ");
$result=array_diff_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>
Result : Array ( [a] => java [c] => php )
array_diff_ukey() | Match arrays, and revert the differences (compare only keys, using a user-defined key comparison function) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"java","b"=>"cloud computing","c"=>"android");
$a2=array("a"=>"java","c"=>"android","d"=>".net");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
</body>
</html>
Result : Array ( [b] => cloud computing )
array_fill() | Stuffs an array with values |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array_fill(3,4,"java");
$b1=array_fill(0,1,"php");
print_r($a1);
echo "<br>";
print_r($b1);
?>
</body>
</html>
Result : Array ( [3] => java [4] => java [5] => java [6] => java )
Array ( [0] => php )
array_fill_keys() | Fills an array with values, specifying keys |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"php");
print_r($a1);
?>
</body>
</html>
Result : Array ( [a] => php [b] => php [c] => php [d] => php )
array_fill_keys() | Fills an array with values, specifying keys |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
function test_odd($var)
{
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>
</body>
</html>
Result : Array ( [3] => 3 )
array_flip() | Exchanges/flips all keys with their associated values in an array |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"java","b"=>"android","c"=>"php","d"=>"html");
$result=array_flip($a1);
print_r($result);
?>
</body>
</html>
Result : Array ( [java] => a [android] => b [php] => c [html] => d )
array_intersect() | Compare arrays, and returns the matches (compare only values) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"php","b"=>"c language","c"=>"java","d"=>"android");
$a2=array("e"=>"php","b"=>" c language ","c"=>" java ");
$result=array_intersect($a1,$a2);
print_r($result);
?>
</body>
</html>
Result : Array ( [a] => php [b] => c language [c] => java )
array_intersect_assoc() | Matches arrays and returns the matches (compare values and keys) |
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"php","b"=>"c language","c"=>"java","d"=>"android");
$a2=array("a"=>"php","b"=>" c language ","c"=>" java ");
$result=array_intersect_assoc($a1,$a2);
print_r($result);
?>
</body>
</html>
Result : Array ( [a] => php [b] => c language [c] => java )
For FREE DEMO CLASS CALL – 011-65164822 / 91- 8860352748
CPD Technologies
Block C 9/8, Sector -7, Rohini, Delhi-110085, India
Landmark: Near Rohini East Metro Station, Opposite Metro Pillar No-397
Website:- www.cpd-india.com
Email :- support@cpd-india.com