Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Difference between array,list and map

Posted on 2011-09-27
7
Medium Priority
?
426 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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