Difference between array,list and map

HI Experts,

I want to know the difference between arrays,list and map and when is best to use them.

Thanks.
dvcphpAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Lukasz ChmielewskiConnect With a Mentor Commented:
In most of the cases you would use an array as it is the simplest way of gathering the values.
You would use the list to assign variables to an array elements, and mapping is useful for appying the function to array elements - square function, addition, any defined.
0
 
Lukasz ChmielewskiCommented:
The difference between array and list is that you can use array with keys which are not varibles, list assigns the array values to list vars, array_map applies the function to the array elements. Take a look at the examples at manual pages:

http://pl2.php.net/manual/en/function.array.php
http://www.php.net/manual/en/function.list.php
http://php.net/manual/en/function.array-map.php
0
 
dvcphpAuthor Commented:
Thanks Genius but I am still confused as to when to use one over the other.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Lukasz ChmielewskiCommented:
The most cases - you use array, basicaly it depends on what you want to do.
Let's make examples:

<?php

// lets's make an array
$a = array(1, 2, 3);

// show me the array
print_r($a);

// let's make a list
list($first, $second, $third) = $a;

// list is like an array keys, but you can acces them with 
// variable name like $first, not with $a[0]
echo $first;

?>

Open in new window

   
0
 
Lukasz ChmielewskiCommented:
Or more complete one:

<?php


function addone($n)
{
    return($n + 1);
}



// lets's make an array
$a = array(1, 2, 3);

// show me the array
print_r($a);

// let's make a list
list($first, $second, $third) = $a;

// list is like an array keys, but you can acces them with 
// variable name like $first, not with $a[0]
echo $first;

$aa = array_map("addone", $a);
print_r($aa);
?>

Open in new window

   
0
 
dvcphpAuthor Commented:
Thank you.You explained it so well.Easy to understand.Thanks again.
0
 
Lukasz ChmielewskiCommented:
Thanks. If any further questions, feel free to ask here.
0
All Courses

From novice to tech pro — start learning today.