Solved

Difference between array,list and map

Posted on 2011-09-27
7
419 Views
Last Modified: 2013-12-13
HI Experts,

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

Thanks.
0
Comment
Question by:dvcphp
  • 5
  • 2
7 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36712414
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
 

Author Comment

by:dvcphp
ID: 36712467
Thanks Genius but I am still confused as to when to use one over the other.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36712516
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36712525
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
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36712542
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
 

Author Closing Comment

by:dvcphp
ID: 36712724
Thank you.You explained it so well.Easy to understand.Thanks again.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36712749
Thanks. If any further questions, feel free to ask here.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question