Most useful Array functions in PHP | Lessoncup Programming Blog

Wednesday, 16 October 2013

Most useful Array functions in PHP

Yesterday i have posted lesson on php string functions. now i am going to explain about php array's and how the array's are useful in web world. PHP arrays allow you to store multiple values in a single variable and operate on them as a set.the most and common array's where php developers will use regularly.


Function 1: Count()

it return's the number of elements from array.

<?php

$lessons=array("JQuery","HTML5","Ajax");

echo count($array);

?>


Function 2: reset()

it return's the first element from array elements.
<?php

$array=array("JQuery","HTML5","Ajax");

echo reset($array);


?>


Function 3: end()

it return's the last element from array elements.

<?php

$array=array("JQuery","HTML5","Ajax");

echo end($array);
?>

Function 4: array_rand()

Pick one or more random entries out of and array If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.

<?php

$array=array("JQuery","HTML5","Ajax");

echo array_rand($array);
?>

Function 4: array_pop()

Return the last value of array.
<?php

$array=array("JQuery","HTML5","Ajax");

echo array_pop($array);
?>

Function 5: array_reverse()

Return the array elements in reverse order.
<?php

$array=array("JQuery","HTML5","Ajax");

echo array_reverse($array);
?>

Function 6: array_merge()

Merge the one or more arrays after merge the merged elements of one of more arrays together so that the values of one are added to the end of the previous array.
the print_r() function use like echo but it is use to print the array elements .
<?php

$array=array("JQuery","HTML5","Ajax");

$array1=array("one","two","three");

print_r(array_merge($array,$array1));
?>

Function 7: array_push()

push the new element onto the end of the previous array and returns the new number of elements in the array.
<?php

$array=array("JQuery","HTML5","Ajax");
array_push($array,"css","php");

print_r($array);
?>

Function 7: range()

create an array with a range of elements.
<?php

$num=range(1,10);

print_r($num);

$let=range('a','z');

print_r($let);
?>

No comments :

Post a Comment

Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA
lessoncup@gmail.com

PHP | My SQL | JQUERY
Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons