4 Array functions you should know in PHP

By

January 3, 2011PHP4 Comments

Level: Beginner

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.

Before going to arrays section,just go through latest posts of Devlup:
5 useful mathematical functions in PHP
How to encrypt a string using one way encryption
String Repeat function in PHP
How to find duplicate words in a string
How to encrypt password using MD5

Following i have explained  about four important array functions which are very useful for beginners.

1) Sort():

This function is used to sort an array by using its key or its value .

<?php

$website=array(“ labnol”,”sml”,”techiemania”,”softwarebuzzer”,”techperk”);

sort($website);

//sort alphabetically by value

print_r($website);

?>

Output :

Array ( [0] => “labnol” [1] => ”sml” [2] => ”softwarebuzzer” [3] => ”techiemania” [4] => ”techperk” )

For associative arrays , use asort() or ksort() ,which sorts by value and key .

a) asort

<?php

$website=array(“labnol” => “amit”,”linuxtree” =>”gautham”,”cybergyaan” =>”jeyaganesh”, “cyberraja” => “ramvibhakar”);

//sorts on value

asort($website);

print_r($website);

?>

Output:

Array ( [“labnol”] => “amit” [“cyberraja”] => “ramvibhakar” [”linuxtree”] => ”gautham” [”cybergyaan”] => ”jeyaganesh” )

b) Ksort()

<?php

$website=array(“labnol” => “amit”,”linuxtree” =>”gautham”,”cybergyaan” =>”jeyaganesh”, “cyberraja” => “ramvibhakar”);

//sorts on key

ksort($website);

print_r($website);

?>

Output :

Array ( [“cyberraja”] => “ramvibhakar” [“labnol”] => “amit” [”cybergyaan”] => ”jeyaganesh” [”linuxtree”] => ”gautham” )

2) Array_reverse():

This function is used to reverse the order of given array .

<?php

$websites=array("devlup","techiemania","shoutmeloud","labnol");

print_r(array_reverse($websites));

?>

Result :

(“labnol”,”shoutmeloud”,”techiemania”,”devlup”)

3) Explode() and Implode()

Explode() is used to convert the strings into arrays ,whereas the Implode() is used to convert the arrays into strings . Let’s see the examples :

Usage of Explode():

<?php

$company=" cts  tcs   csc  patni  google  microsoft ";

print_r(explode("  " , $company));

?>

There are two parameters in Explode function :

1) Separator.

2) Variable (which consists of strings ).

Above i have used whitespace as separator .

Result : (“cts”,”tcs”,”csc”,”patni”,”google”,”microsoft”)

Usage of Implode():

Implode() does the opposite of Explode().It converts the arrays into strings .

This function is used to combine array elements to form a string .

<?php

$company= ("cts","tcs","csc","patni","google","microsoft");

echo implode(" is  " , $company);

?>

Result :

cts is tcs is csc is patni is google is microsoft

4) array_slice():

This function is used to extract subsection of an array .It acts similar to substr().

It takes three parameters .

a) array variable

b) where the extraction should start

c) number of elements which we are going to extract from an array

Lets see an example :

<?php

$company=array("csc","cts","tcs","ford","hcl");

$extract=array_slice($company,0,2);

print_r($extract);

<?

Result : csc,cts

You can follow us on Twitter or join our Facebook Fan Page for more updates like this

4 Responses to “4 Array functions you should know in PHP”
  1. Ricardo

    Great tips!
    The function array_map is really useful too.

  2. N.S Gautham Raj

    Thanks Ricardo for sharing with us !!! I will include that in next array related article !!

  3. Montreal Web Design

    Yep, I use them all the time too.

  4. Gautham

    @Montreal Thanks for commenting !! Do you use any special array functions , Please share with us , it would be useful .( we will be including in the next article ).

Leave a Reply

*