PHP Array Functions

In this tutorial we will learn some of the important functions used with array. Let’s start with the most important function which is used for creating array variable.

array()

In PHP an array can be created using a function array() as shown in the example.

For Ex.

<?php
    $num=array(10,20,30,40,50);
?>

is_array()

This function is used to check whether the given variable is an array or not. It will return 1(true) or false (0) . If the variable given in argument is array than it will return 1 otherwise 0.

 Syntax

is_array(variable_name)

For Ex.

<?php
    $student=array(1,"Pratik","Gujarat");
    $str="DiscussDesk";
    echo is_array($student)."<br>";
    echo is_array($str)."<br>";
?>

Output

1
0

count()

This function will count the number of elements present in an array.

Syntax

count(array_variable)

For Ex.

<?php
    $student=array("Deep","Pratik","Deepen");
    echo "Total Elements in Array ".count($student);
?>

Output

Total Elements in Array 3

in_array()

This function is used to check if the given value exists in the array or not. It returns 1 if the value exists in array 0 otherwise.

Syntax

in_array(value to be searched,array_variable)

For Ex.

<?php
    $student=array("Deep","Pratik","Deepen");
    if(in_array("Deep",$student))
    {
       echo "Deep is a Student";
    }
?>

Output

Deep is a Student

current() , next() , prev()

current() function will give the current item from the array. The starting position of the array will be first element in array so current() will return the first element for the first time.

next() function will forward the position in an array to the next item. It will return the next element in array.

prev() function will move backward in an array. It will return the previous element in array.

Syntax

current(Array_Variable)
next(Array_Variable)
prev(Array_Variable)

For Ex.

<?php
    $student=array("Deep","Pratik","Deepen");
    echo "Current Element ".current($student)."<br>";
    echo "Next Element ".next($student)."<br>";
    echo "Previous Element ".prev($student)."<br>";
?>

Output

Current Element Deep
Next Element Pratik
Previous Element Deep

reset()

This function will reset the array pointer to the first position (i.e the first element) in array.

Syntax

reset(Array_Variable)

end()

This function will move the array pointer to the last element of the array.

Syntax

end(Array_Variable)

For Ex.

<?php
    $student=array("Deep","Pratik","Deepen");
    next($student);
    next($student);
    echo "Current Element ".current($student)."<br>";
    reset($student);
    echo "Current Element ".current($student)."<br>";
    end($student);
    echo "Current Element ".current($student)."<br>";
?>

Output

Current Element Deepen
Current Element Deep
Current Element Deepen

sort()

This function will sort the array elements in an array in ascending order. Note that the index of the items gets changed using sort function.

Syntax

sort(array_name)

For Ex.

<?php
    $student=array("Deep","Pratik","Jainam","Rishi");
    echo "Before Sorting<br>";
    print_r($student);
    sort($student);
    echo "<br>after Sorting<br>";
    print_r($student);
?>

Output

Before Sorting
Array ( [0] => Deep [1] => Pratik [2] => Jainam [3] => Rishi )
after Sorting
Array ( [0] => Deep [1] => Jainam [2] => Pratik [3] => Rishi )

rsort()

This function is similar to that of sort() except it will sort the elements of array in descending order(reverse).

Syntax

rsort(array_name)

For Ex.

<?php
    $student=array("Deep","Pratik","Jainam","Rishi");
    echo "Before Sorting<br>";
    print_r($student);
    rsort($student);
    echo "<br>after Sorting<br>";
    print_r($student);
?>

Output

Before Sorting
Array ( [0] => Deep [1] => Pratik [2] => Jainam [3] => Rishi )
after Sorting
Array ( [0] => Rishi [1] => Pratik [2] => Jainam [3] => Deep )

asort()

It’s abbreviated as associative sort. This function will also sort array elements in ascending order the only difference is that it will not change the index of the element. The index remains as it is.

Syntax

asort(array_name)

For Ex.

<?php
    $student=array("Deep","Pratik","Jainam","Rishi");
    echo "Before Sorting<br>";
    print_r($student);
    asort($student);
    echo "<br>After Sorting<br>";
    print_r($student);
?>

Output

Before Sorting
Array ( [0] => Deep [1] => Pratik [2] => Jainam [3] => Rishi )
After Sorting
Array ( [0] => Deep [2] => Jainam [1] => Pratik [3] => Rishi )

arsort()

It’s abbreviated as associative reverse sort. This function will sort array elements in descending order  it will not change the index of the element. The index remains as it is.

Syntax

arsort(array_name)

Practice one example on your own refer the example for asort() function.

array_merge()

This function is used to merge more than one array variables. The return type is array itself.

Syntax

array_merge(array_var1,array_var2,…)

For Ex.

<?php
    $student1=array("Deep","Pratik","Jainam");
    $student2=array("Deepen","Ravi","Rishi");
    $student=array_merge($student1,$student2);
    print_r($student);
?>

Output

Array ( [0] => Deep [1] => Pratik [2] => Jainam [3] => Deepen [4] => Ravi [5] => Rishi )

print_r()

This function will display the array in the format as it is declared. The return type of print_r() function is void.

Syntax

print_r(Array_Variable)

We have used print_r() function in many of the examples shown above.

To conclude in this tutorial we have learnt some of the important functions used with array.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments