Monday, 5 August 2019

PHP array functions

In this post , we will be discussing the php array functions. As with any other programming language,PHP has its own array functions

What is array: A array is the chain of values stored in single variable where values can be retrived based on there position in the array
$arr ={a,b,c,d,e,f};
There are three type of array in PHP
Indexed arrays – Arrays with numeric index
Syntax
$arr ={a,b,c,d,e,f};
The values are retrieved based on the numerical index
<?php
$arr ={a,b,c,d,e,f};
print “First element is $arr[0]”;
print “second element is $arr[1]”;
print “Third element is $arr[2]”;
print “Fourth element is $arr[3]”;
print “Fifth element is $arr[4]”;
?>
Associative arrays – Arrays with named keys
Syntax
array(key=>value,key=>value,key=>value,etc.);
$age = array (
Tom=>”34″,
rony=>”36″,
john=>”35″,
);
The values are retreived based on named keys
<?php
$age = array (
Tom=>”34″,
rony=>”36″,
john=>”35″,
);
print “Tom age is $age[Tom]”;
print “Rony age is $age[rony]”;
print “John age is $age[john]”;
?>
Multidimensional arrays – Arrays containing one or more arrays. The array contains multiple arrays
$house= array(
array ( a,b,c,d),
array ( 1,2,3,4),
array ( A,B,C,D)
)
The values are retreived based on two keys
<?php
$house= array(
array ( a,b,c,d),
array ( 1,2,3,4),
array ( A,B,C,D)
)
print ” First array and first element is $house[0][0]”;
print “First array and second element is $house[0][1]”;
print “second array and second element is $house[1][1]”;
print “Third array and second element is $house[2][1]”;
?>
How to count the elements in the array and retrive it dynamically
The count in a array can be done using the count function
count(array,mode);
<?php
$arr=array(“a”,”b”,”c”);
echo count($arr);
?>
We can loop through all the elements and print it
<?php
$test=array(“a”,”b”,”c”);
$arroflength=count($test);
for($x=0;$x<$arroflength;$x++)
{
echo $test[$x];
echo “<br>”;
}
?>

Important php array functions
sizeof($arr)  : It is same as count($arr).  It is use to count the members in the array  which then can be used in loop to process all the array element
<?php
$arr=array(“e”,”f”,”g”);
echo sizeof($arr);
?>
We can loop through all the elements and print it
<?php
$test=array(“e”,”f”,”g”);
$arroflength=sizeof($test);
for($x=0;$x<$arroflength;$x++)
{
echo $test[$x];
echo “<br>”;
}
?>
array_values($arrtest)  and array_keys($arrtest):
array_values  function accepts a PHP array and returns a new array containing only its values (not its keys). Its counterpart is the array_keys() function.
This can be use this function to retrieve all the values from an associative array.
array_keys  function accepts a PHP array and returns a new array containing only its keys.
This can be use this function to retrieve all the keys from an associative array.
<?php
$age = array (
Tom=>”34″,
rony=>”36″,
john=>”35″,
);
print_r(array_values($age));
?>
The above code will print array having the values
<?php
$age = array (
Tom=>”34″,
rony=>”36″,
john=>”35″,
);
print_r(array_keys($age));
?>
The above code will print array having the keys
array_unique($arraytest):
This function strip the duplicate data from the array
array_pop($arraytest)
This function removes the element from the  end of array
array_push($arraytest, $values)
This  function add the element stored in $values in the end to the array
array_shift($arraytest)
This function removes the element from the  start of array
array_unshift($arraytest, $values)
This  function add the element stored in $values in the start to the array

0 comments:

Post a Comment