Home » Web development » PHP » PHP array functions

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 retrieved 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 retrieve 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);
?>

How to print the elements in the array

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)  Function

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)  Function

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.

See also  How to define constant in php

This can be use this function to retrieve all the values from an associative array.

The below code will print array having the values

<?php
$age = array (
Tom=>"34",
rony=>"36",
john=>"35",
);

print_r(array_values($age));
?>

Array ( [0] => 34 [1] => 36 [2] => 35 )

array_keys($arrtest) Function

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_keys($age));
?>

Array ( [0] => Tom [1] => rony [2] => John )

The above code will print array having the keys

array_unique($arraytest) function

This function strip the duplicate data from the array

<?php
$age = array (
Tom=>"34",
rony=>"36",
john=>"34",
);

print_r(array_unique($age));
?>

Array ( [tom] => 34 [rony] => 36  )

array_pop($arraytest)

This function removes the element from the  end of array

<?php
$age = array (
Tom=>"34",
rony=>"36",
john=>"34",
);
array_pop($age)
print_r($age);
?>

Array ( [tom] => 34 [rony] => 36 )

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

I hope you like this detailed post on PHP Array functions. Please do provide the feedback

Also Reads
How to run php file in windows : This post talks about how we can php file on the the window and do the testing before moving to the web server
uses of PHP : This has detailed description why PHP is so much used through out the world.
first php program : This is good starting point for anybody starting with PHP and talk in good detail about how to write your PHP program and execute it
variables in PHP
Define constant in PHP
https://en.wikipedia.org/wiki/PHP

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top