Solved

Difference between array,list and map

Posted on 2011-09-27
7
414 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
Comment Utility
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
Comment Utility
Thanks Genius but I am still confused as to when to use one over the other.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.You explained it so well.Easy to understand.Thanks again.
0
 
LVL 27

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…

762 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

15 Experts available now in Live!

Get 1:1 Help Now