Solved

Difference between array,list and map

Posted on 2011-09-27
7
416 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP extract HTML using DOMXpath from nested tables 8 47
Apache/PHP: Cannot make phpinfo() and phpmyadmin work 4 34
Php pie charts 3 26
only allow numbers with preg match 4 25
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now