vijayjoshi.org - php | javascript | ajax | and all things web

Php array functions

As you know php has a large array of array functions.While some of these are used frequently, some are lesser known but even they can be very useful in certain situations.We will analyze 4 such functions and see how they can be useful.
The functions we will be exploring are range,list,compact and extract

1- range

range takes min & max as 2 parameters and returns an array of variables from min to max.

print_r (range(1, 10));

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

A third optional parameter is step.It was introduced in php5.

print_r (range(20, 5, 5));

Output:

Array
(
    [0] => 20
    [1] => 15
    [2] => 10
    [3] => 5
)

Note that when min is greater then max,the results are also in reverse order.
Does range works only for numbers? Of course not. You can use it with alphabets too.

print_r (range('a', 'z', 5));

Output:

Array
(
    [0] => a
    [1] => f
    [2] => k
    [3] => p
    [4] => u
    [5] => z
)

2- list

Consider this example

list($a, $b, $c) = array('one', 'two');

var_dump($a);
var_dump($b);
var_dump($c);

Output:

string(3) "one"
string(3) "two"
NULL

Note that an array element will be set to null if there is no value for it.
This may be extremely useful when you want to assign multiple values retrieve from database to respective variables.
A query like this:

$userResultSet =  mysql_query(select name,age,city,country from userInfo where userId="1",$connection);

will require you to create 4 variables for name,age,city and country.
With list() this can be done in one go

list($userName,$age,$city,$country) = $userResultSet;

You can also assign values to array instead of plain variables.

3- compact

It takes strings or array of string(s), then looks for a varibale name same as the string in the current symbol table.If a varibale is found it creates an associative array where key being the string passed and value being the value of that variable.
If a variable name is not found compact just skips it.

$me = 'vijay';
$myFriend = 'tom';
$myOtherFriend = 'paul';

print_r(compact('me', 'myOtherFriend', 'myFriend', 'noFriend'));

Output:

Array
(
    [me] => vijay
    [myOtherFriend] => paul
    [myFriend] => tom
)

In the above example, value to variables me,myFriend & myOtherFriend have been assigned, so the resulting array have only these 3. Rest are skipped.

4- extract

You can think of it as opposite of compact. It takes an associative array and creates variables in current symbol table. Array keys are variable names and array values are values for the variable.

$arr = array('one' => 'thisisONE', 'two' => 'thisisTWO');
extract($arr);
echo$one;
echo$two;

Output:

thisisONE
thisisTWO

We saw that 2 variables $one and $two were created.
What if a variable $one already exists. Will its value be overwritten or not?
For this there are 2 more parameters to extract. These are extract_type and prefix.
extract_type can be one of the multiple values.
Some commonly used values are – EXTR_OVERWRITE,EXTR_SKIP,EXTR_PREFIX_SAME,EXTR_PREFIX_ALL
In case of collision with existing variable names
EXTR_OVERWRITE overwrites any existing variable in symbol table.
EXTR_SKIP skips and does not overwrite
EXTR_PREFIX_SAME prefixes the newly created variable with prefix and
EXTR_PREFIX_ALL prefixes all variables with prefix

$one = 'thisisOriginalOne';
$arr = array('one' => 'thisisONE', 'two' => 'thisisTWO');
extract($arr,EXTR_PREFIX_SAME,"newVar");
echo$one;
echo$two;
echo$newVar_one;

Output:

thisisOriginalOne
thisisTWO
thisisONE

Related Posts

Posted in : PHP
Tags:

3 Comments to “Php array functions”

Add Comments (+)

  1. Mojo says:

    Nice article Vijay!
    You’ve catered various possibilities and uses of arrays so easily. I am a naive to PHP at the moment. It would b great if you could provide some bottom level crucial information which might help a starter like me.

  2. Dorababu says:

    Good work dude.. Hi i need PHP code regarding Datbase connection can u mail me some please

  3. Vijay says:

    sure mate. Please tell me in detail, what code do you want

Leave a Reply

Leave a Reply

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

*

* Copy this password:

* Type or paste password here:

55,809 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>