Monday, 27 August 2018

Array & Its Functions in PHP

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 assignThe 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.
NameStockSold
Hero52
BSA1513
Atlas2218
Hercules1715

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 )

array_intersect_key()Compare arrays, and returns the matches (compare keys only)
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"php","b"=>"java","c"=>"android");
$a2=array("a"=>"php","c"=>"android","d"=>"cloud computing");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
</body>
</html>
Result: Array ( [a] => php [c] => android )

array_intersect_uassoc()Distinct arrays, and returns the matches (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"=>"cloud computing","b"=>"php","c"=>"android");
$a2=array("d"=>"cloud computing","b"=>"php","e"=>"android");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>
Result : Array ( [b] => php )

array_intersect_ukey()Distinct arrays, and returns the matches (compare only keys, using a user-defined key comparison function)
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"php","b"=>"html","c"=>"java");
$a2=array("a"=>"php","c"=>"java","d"=>"android");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
</body>
</html>
Result : Array ( [a] => php [c] => java )

array_key_exists()Examine if the specified key exists in the array
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("java"=>"version 8","php"=>"version 6");
if (array_key_exists("java",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
</body>
</html>
Result : Key exists!

array_keys()Returns all the keys of an array
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("dotnet"=>"version 4.6","php"=>"version 6","java"=>"version 8",);
print_r(array_keys($a));
?>
</body>
</html>
Result : Array ( [0] => php [1] => java [2] => dotnet )

array_map()Sends each value of an array to a user-define function, which returns new values
Example:
<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($num)
{
return($num*$num);
}
$a=array(4,7,2,15,13);
print_r(array_map("myfunction",$a));
?>
</body>
</html>
Result: Array ( [0] => 16 [1] => 49 [2] => 4 [3] => 225 [4] => 169 )

array_merge()One or more arrays will merges into one array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("php","android");
$a2=array("C++","java");
print_r(array_merge($a1,$a2));
?>
</body>
</html>
Result: Array ( [0] => php [1] => android [2] => C++ [3] => java )

array_merge_recursive()One or more arrays will merges into one array recursively
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"android","b"=>"java");
$a2=array("c"=>"dotnet","b"=>"php");
print_r(array_merge_recursive($a1,$a2));
?>
</body>
</html>
Result: Array ( [a] => android [b] => Array ( [0] => java [1] => php ) [c] => dotnet )

array_multisort()Sorts multi-dimensional or multiple arrays
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("Seo","Data structure","Java","Android","Php");
array_multisort($a);
print_r($a);
?>
</body>
</html>
Result: Array ( [0] => Android [1] => Data structure [2] => Java [3] => Php [4] => Seo )

array_pad()Inserts a predefined number of items, with a predefined value, to an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("java","php");
print_r(array_pad($a,5,"android"));
?>
</body>
</html>
Result: Array ( [0] => java [1] => php [2] => android [3] => android [4] => android )

array_pop()Eleminates the last element of an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("dotnet","php","html");
array_pop($a);
print_r($a);
?>
</body>
</html>
Result: Array ( [0] => dotnet [1] => php )

array_product()Measures the product of the values in an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array(5,5);
echo(array_product($a));
?>
</body>
</html>
Result: 25

array_push()Fills one or more elements to the end of an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("dotnet","php");
array_push($a,"html","java");
print_r($a);
?>
</body>
</html>
Result: Array ( [0] => dotnet [1] => php [2] => html [3] => java )

array_rand()Returns one or more random keys from an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("seo","java","php","android","C++");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];
?>
</body>
</html>
Result: seo
java
php

array_reduce()Using a user-defined function, returns an array as a string
Example:
<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("php","android","java");
print_r(array_reduce($a,"myfunction"));
?>
</body>
</html>
Result: php-android-java

array_replace()changes the values of the first array with the values from following arrays
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("android","C++");
$a2=array("php","java");
print_r(array_replace($a1,$a2));
?>
</body>
</html>
Result: Array ( [0] => php [1] => java )

array_replace_recursive()Changes the values of the first array with the values from following arrays recursively
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>array("java"),"b"=>array("php","android"),);
$a2=array("a"=>array("data structure"),"b"=>array("cloud computing"));
print_r(array_replace_recursive($a1,$a2));
?>
</body>
</html>
Result: Array ( [a] => Array ( [0] => data structure ) [b] => Array ( [0] => cloud computing [1] => android ) )

array_reverse()Returns an array in the reverse order
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"cloud computing","b"=>"data structure","c"=>"C++");
print_r(array_reverse($a));
?>
</body>
</html>
Result: Array ( [c] => cloud computing [b] => BMW [a] => C++ )

array_search()finds an array for a given value and returns the key
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"java","b"=>"php","c"=>"android");
echo array_search("java",$a);
?>
</body>
</html>
Result: a

array_shift()Eliminates the first element from an array, and returns the value of the eliminates element
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"cloud computing","b"=>"java","c"=>"data structure");
echo array_shift($a)."<br>";
print_r ($a);
?>
</body>
</html>
Result: cloud computing
Array ( [b] => java [c] => data structure )

array_slice()Returns choosen parts of an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("php","cloud computing","dotnet","java","android");
print_r(array_slice($a,2));
?>
</body>
</html>
Result: Array ( [0] => dotnet [1] => java [2] => android )

array_splice()Eliminates and substitutes specified elements of an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"C++","b"=>"dotnet","c"=>"html","d"=>"wordpress");
$a2=array("a"=>"data structure","b"=>"android");
array_splice($a1,0,2,$a2);
print_r($a1);
?>
</body>
</html>
Result: Array ( [0] => data structure [1] => android [c] => html [d] => wordpress )

array_sum()Returns the total of the values in an array
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$a=array(22,56,94);
echo array_sum($a);
?>
</body>
</html>
Result: 172

array_udiff()Distinct arrays, and returns the differences (compare only values, 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"=>"seo","b"=>"data structure","c"=>"android");
$a2=array("a"=>"android","b"=>"java","e"=>"android");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>
Result: Array ( [a] => seo [b] => data structure )
.
array_udiff_assoc()Distinct arrays, and returns the differences (compare values and keys, using a built-in function to compare the keys and a user-defined function to compare the values)
Example:
<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"data structure","b"=>"html","c"=>"android");
$a2=array("a"=>"data structure","b"=>"android","c"=>"html");
$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>
Result: Array ( [b] => html [c] => android )

array_udiff_uassoc()Distinct arrays, and returns the differences (compare values and keys, using two user-defined key comparison functions)
Example:
<!DOCTYPE html>
<html>
<body>
<?php
function myfunction_key($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
function myfunction_value($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"java","b"=>"android","c"=>"html");
$a2=array("a"=>"java","b"=>"android","c"=>"android");
$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);
?>
</body>
</html>
Result: Array ( [c] => html )

0 comments:

Post a Comment