Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Useful PHP Functions To Work With Arrays (http://www.go4expert.com/articles/useful-php-functions-arrays-t7259/)

pradeep 12Nov2007 12:04

Useful PHP Functions To Work With Arrays
 
The in-built array functions allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing, and operating on sets of variables.

Simple and multi-dimensional arrays are supported, and may be either user created or created by another function. There are specific database handling functions for populating arrays from database queries, and several functions return arrays.

You can read more about working with PHP arrays here.

In this article I'll demonstrate a few important and useful php functions, you can read and refer to the whole list of PHP functions on the official PHP website.

array_push()



Inserts one or more elements to the end of an array.

Code: PHP

$arr = array('Shabbir','Pradeep');
 
 //Add one more element
 array_push($arr,'Tanaz');
 
 print_r($arr);
 
 /*
 Output:
 Array
 (
     [0] => Shabbir
     [1] => Pradeep
     [2] => Tanaz
 )
 */

array_pop()



Deletes the last element of an array.

Code: PHP

$stack = array("orange", "banana", "apple", "raspberry");
 $fruit = array_pop($stack);
 print_r($stack);
 
 /*
 After this, $stack will have only 3 elements:
 Array
 (
     [0] => orange
     [1] => banana
     [2] => apple
 )
 
 and raspberry will be assigned to $fruit.
 */

array_shift()



Removes the first element from an array, and returns the value of the removed element.

Code: PHP

$stack = array("orange", "banana", "apple", "raspberry");
 $fruit = array_shift($stack);
 print_r($stack);
 
 /*
 This would result in $stack having 3 elements left:
 Array
 (
     [0] => banana
     [1] => apple
     [2] => raspberry
 )
 
 and orange will be assigned to $fruit.
 */

array_unshift()



Adds one or more elements to the beginning of an array.

Code: PHP

$queue = array("orange", "banana");
 array_unshift($queue, "apple", "raspberry");
 print_r($queue);
 
 
 /*
 Output
 Array
 (
     [0] => apple
     [1] => raspberry
     [2] => orange
     [3] => banana
 )
 */

array_unique()



Removes duplicate values from an array.array_unique() takes input array and returns a new array without duplicate values.

Code: PHP

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
 $result = array_unique($input);
 print_r($result);
 
 /*
 Output
 Array
 (
     [a] => green
     [0] => red
     [1] => blue
 )
 */

in_array()



Checks if a value exists in an array.

Code: PHP

$os = array("Mac", "NT", "Irix", "Linux");
 if (in_array("Irix", $os))
 {
     echo "Got Irix";
 }
 if (in_array("mac", $os))
 {
     echo "Got mac";
 }
 
 /*
 The second condition fails because in_array() is case-sensitive, so the program above will display:
 
 Got Irix
 */

array_walk()



Apply a user function to every member of an array.

Code: PHP

$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
 
 function test_alter(&$item1, $key, $prefix)
 {
     $item1 = "$prefix: $item1";
 }
 
 function test_print($item2, $key)
 {
     echo "$key. $item2<br />\n";
 }
 
 echo "Before ...:\n";
 array_walk($fruits, 'test_print');
 
 array_walk($fruits, 'test_alter', 'fruit');
 echo "... and after:\n";
 
 array_walk($fruits, 'test_print');
 
 /*
 The above example will output:
 Before ...:
 d. lemon
 a. orange
 b. banana
 c. apple
 ... and after:
 d. fruit: lemon
 a. fruit: orange
 b. fruit: banana
 c. fruit: apple
 */

array_combine()



Creates an array by using one array for keys and another for its values.

Code: PHP

$a = array('green', 'red', 'yellow');
 $b = array('avocado', 'apple', 'banana');
 $c = array_combine($a, $b);
 
 print_r($c);
 
 /*
 The above example will output:
 
 Array
 (
     [green]  => avocado
     [red]    => apple
     [yellow] => banana
 )
 */

end()



Sets the internal pointer of an array to its last element.This function can be used to get the last element of an array.

Code: PHP

$fruits = array('apple', 'banana', 'cranberry');
 echo end($fruits); // cranberry
 

These functions make our work a bit more easier, I consider these are the most frequently used array functions, but this is not all that PHP offers, a lot more can be found at the PHP website.

trinitybrown 25Oct2008 11:53

Re: Useful PHP Functions To Work With Arrays
 
ALl scripts are very fine, do you have some idea about associative arrays and ksort() function as i tried to learn it but was unable to understand it , kindly help me on the topic

pradeep 25Oct2008 14:26

Re: Useful PHP Functions To Work With Arrays
 
What exactly do you want to know?

happyz 22Nov2008 19:36

Re: Useful PHP Functions To Work With Arrays
 
Its a nice post abt PHP array functions
really like it

haileyjonathan 10Jun2009 17:01

Re: Useful PHP Functions To Work With Arrays
 
Hi Pradeep,
You wrote a wonderful scripts.It included PHP array function also. I appreciate to you..Thanks for your sharing also...

LenoxFinlay 17Jun2009 05:31

Re: Useful PHP Functions To Work With Arrays
 
There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value.PHP internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is what the keys happen to be. Some array features are provided mainly for use with indexed arrays, because they assume that you have or want keys that are consecutive integers beginning at 0. In both cases, the keys are unique--that is, you can't have two elements with the same key, regardless of whether the key is a string or an integer.PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. The order is normally that in which values were inserted into the array, but the sorting functions described later let you change the order to one based on keys, values, or anything else you choose.

gkumar 19Jun2009 15:33

Re: Useful PHP Functions To Work With Arrays
 
Like arrays in other languages, PHP arrays allow you to store multiple values in a single variable and operate on them as a set. PHP offers an extensive array manipulation toolkit—over 60 functions—that lets you process arrays in almost any way imaginable including reversing them, extracting subsets, comparing and sorting, recursively processing, and searching them for specific values.
his document outlines some of the more useful functions in the PHP array with explanations and usage examples:
Function:-
sizeof($arr)
array_values($arr)
array_keys($arr)
array_pop($arr)
array_push($arr, $val)
array_shift($arr)
array_unshift($arr, $val)
each($arr)
sort($arr)
array_flip($arr)
array_reverse($arr)
array_merge($arr)
array_rand($arr)
array_search($search, $arr)
array_slice($arr, $offset, $length)
array_unique($data)array_walk($arr, $func)

lrvv 7Sep2009 17:21

Re: Useful PHP Functions To Work With Arrays
 
ok,thx for your sharing

qforever 25Jan2010 20:23

Re: Useful PHP Functions To Work With Arrays
 
Here is much more detailed description of PHP array functions: php.net/manual/en/book.array.php

qforever 25Jan2010 20:25

Re: Useful PHP Functions To Work With Arrays
 
Just to kill this topic:)

loyo 12Feb2010 20:06

Re: Useful PHP Functions To Work With Arrays
 
good, thanks for your sharing.
make a mark.

loyo 23Feb2010 22:40

Re: Useful PHP Functions To Work With Arrays
 
thank you for you share, keep you up.

webdeveloperr 25May2010 19:23

Re: Useful PHP Functions To Work With Arrays
 
its really nice and useful post..
Thanks

ichandu 29Nov2010 14:04

Re: Useful PHP Functions To Work With Arrays
 
hi pradeep,

This is good contribution from you.I really appreciate to you.Function is the strong part of any program.
I am very much helpful to share a nice article.
Thanks,

---------------------------------

pein87 30Dec2010 01:18

Re: Useful PHP Functions To Work With Arrays
 
You can use $myArray[] = "values" instead of using array_push or array merge because they require more resources to use them(overhead of making a function call).


All times are GMT +5.5. The time now is 20:48.