...

View Full Version : each() and list()



Muhammad Haris
04-29-2006, 12:47 AM
Hello,

I'm new to PHP and reading a book called PHP and mySQL Web Development 2nd Edition.Its is great so far and i've completed 2 chapters and made a nice little script for myself! I'm proud on that! :D

Now I've reached arrays() chapter which is really really confusing me! I can't understand the exact use of each() and list() and what does it do and how can it help me make scripts.

I checked PHP Manual and it didnt exactly explained me and i am confused in several PHP terminologies as well but i can ask them later.

If anyone can example me the use,how can we use in scripts and examples of arrays(), each() and list() functions which should act like a script so i can understand. I would really appriciate it!

Regards,
Muhammad Haris

:)

marek_mar
04-29-2006, 01:03 AM
I must say that these functions aren't really useful.
list() isn't a function.
You can use list() to make varibles out of array values.


$array = array('a', 'b', 'c');
list($letter_a, $letter_b, $letter_c) = $array;

Now $letter_a will hold "a" and so on.

each() gives an alternative way to loop through arrays.

Muhammad Haris
04-29-2006, 01:07 AM
I must say that these functions aren't really useful.
list() isn't a function.
You can use list() to make varibles out of array values.


$array = array('a', 'b', 'c');
list($letter_a, $letter_b, $letter_c) = $array;

Now $letter_a will hold "a" and so on.

each() gives an alternative way to loop through arrays.

That's explains a lot to me and i got the list() preety well and its useful because it turns the array into variable which can be used in scripts(will make code more readable).

Anyways, I'm still confused for each().

Regards,
Muhammad Haris

chump2877
04-29-2006, 01:45 AM
I prefer using foreach (http://us2.php.net/manual/en/control-structures.foreach.php) to traverse arrays, instead of each()...

marek_mar
04-29-2006, 03:24 AM
That's explains a lot to me and i got the list() preety well and its useful because it turns the array into variable which can be used in scripts(will make code more readable).

You can't turn an array into a variable. Variables store data of some type and an array is a way to store data. But I know what you mean :P and you shouldn't need to do that too often.

missing-score
04-29-2006, 04:13 AM
Yeah I see these functions used all the time in instruction book but they are really not as useful as the books seem to suggest.

Muhammad Haris
04-29-2006, 01:52 PM
Last Night I read about foreach() and it was preety intresting. The list() and foreach() looks usuable in scripts but i would like to find out that if the following code will store in $a[0] the element 1?



<?php
$a = arrays(1,2,3,4,5,6,7,8,9,10)
foreach ($a as $b){
echo '$b';
}
?>


Regards,
Muhammad Haris

missing-score
04-29-2006, 02:48 PM
Take this example:



<?php

$array = array( 'One', 'Two', 'Three', 'Four', 'Five' );

foreach( $array as $number ){
echo "$number<br />";
}

?>


If you look at the output, you should see each number on a new line. foreach is very usable in scripts, I use it all the time for traversing arrays.

Muhammad Haris
04-29-2006, 08:01 PM
Take this example:



<?php

$array = array( 'One', 'Two', 'Three', 'Four', 'Five' );

foreach( $array as $number ){
echo "$number<br />";
}

?>


If you look at the output, you should see each number on a new line. foreach is very usable in scripts, I use it all the time for traversing arrays.

Exactly and thanks for the example! :)

EDIT : Okay Now I'm on 109th page of 900 pages of my book and i'm happy about it but now i'm stuck at associative arrays. Consider this as my example


<?php
$Prices['HTML'] = 15;
$Prices['CSS'] = 25;
$Prices['Ajax'] = 4;

foreach($prices as $key => $value){
echo $key.'=>'.$value.'<br />';
}
?>


I can understand the code but my question is that $key in the foreach loops mean the keys which is HTML,CSS,Ajax but how come the $value is used in the code? is $value a variable that is there in php from default from arrays and what does it helps to do!

Regards,
Muhammad Haris

missing-score
04-29-2006, 09:09 PM
Using this kind of array traversal allows you to grab the key as well as the value... You probably already got this far. You can then use the key and or the value if you need it. For example, say you had some links with URLs and Text:




$urls = array();
$urls['http://www.codingforums.com/'] = 'Coding Forums';
$urls['http://www.phpweekly.net/'] = 'PHP Weekly';
$urls['http://www.digg.com/'] = 'Digg.com';

foreach( $urls as $url => $text ){
echo '<a href="' . $url . '">' . $text . '</a>';
}



This would produce a list of three links.

Muhammad Haris
04-29-2006, 09:23 PM
Using this kind of array traversal allows you to grab the key as well as the value... You probably already got this far. You can then use the key and or the value if you need it. For example, say you had some links with URLs and Text:




$urls = array();
$urls['http://www.codingforums.com/'] = 'Coding Forums';
$urls['http://www.phpweekly.net/'] = 'PHP Weekly';
$urls['http://www.digg.com/'] = 'Digg.com';

foreach( $urls as $url => $text ){
echo '<a href="' . $url . '">' . $text . '</a>';
}



This would produce a list of three links.

:thumbsup: Thanks but now could you give me the example of multi-dimension array and three-dimension array illustrating the same example but in expanded form means large examples which can be used in scripts with foreach loop example.

Regards,
Muhammad Haris

trib4lmaniac
04-29-2006, 10:17 PM
Multi-dimensional arrays aren't used all that often in practice. Here's a pointless example of a 2D one that'll will print out your times tables:


$a = array(
1 => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
);

echo "<pre>";
foreach($a as $key => $b) {
foreach($b as $c) {
echo str_pad($c * $key, 3) . " ";
}
echo "\n";
}
echo "</pre>";

Note that $a could have been filled much more easily:


$a = array();
for($i = 1; $i <= 12; $i++) {
$a[$i] = array();
for($j = 1; $j <= 12; $j++)
$a[$i][] = $j;
}

Muhammad Haris
04-29-2006, 11:25 PM
Well some more examples for this multi-dimension and three-dimension arrays with foreach and array traversal would allow me to learn better!

The above code made me a little bit of confused!

trib4lmaniac
04-29-2006, 11:46 PM
Well some more examples for this multi-dimension and three-dimension arrays with foreach and array traversal would allow me to learn better!

The above code made me a little bit of confused!
Yeah, though it might. Try this on for size:


$names = array(
'male' => array(
'a' => array('Alex', 'Andrew', 'Alan'),
'c' => array('Charles', 'Cartman'),
'r' => array('Robert', 'Ronald', 'Rick')
),
'female' => array(
'a' => array('Abigail', 'Anita'),
'b' => array('Betty', 'Beatrice'),
'e' => array('Ellen', 'Eucalyptus', 'Euginia')
)
);

// Print out all boys names beginning with a.
foreach($names['male']['a'] as $name) {
echo $name . "\n";
}

echo "-----\n";
// Print out all girls names.
foreach($names['female'] as $name_list) {
foreach($name_list as $name) {
echo $name . "\n";
}
}

// Print out all boys names that don't begin with a.
echo "-----\n";
foreach($names['male'] as $letter => $name_list) {
if($letter != 'a') {
foreach($name_list as $name) {
echo $name . "\n";
}
}
}

// Print out some debugging info about the entire $names variable
echo "-----\n";
print_r($names);

missing-score
04-29-2006, 11:48 PM
trib4lmaniac, why dont you include full braces in your example eg: foreach(...){}, becuase it makes it much more human readable and is most likley what Muhammad Haris is used to.

trib4lmaniac
04-29-2006, 11:51 PM
It's a matter of opinion, but I suppose you're right. I'll bung 'em in.

Muhammad Haris
04-30-2006, 12:13 AM
trib4lmaniac, why dont you include full braces in your example eg: foreach(...){}, becuase it makes it much more human readable and is most likley what Muhammad Haris is used to.

exactly and big thanks to the helpers on this thread on my quest on learning php! PHP is preety awsome and my experience in learning PHP is great!

I hope to become a reliable php programmer soon!

Regards,
Muhammad Haris

missing-score
04-30-2006, 12:18 AM
I hope to become a reliable php programmer soon!

Ive been coding with PHP for a bit longer than 3 years now... You have alot to learn, but its enjoyable to learn and once you learn these things there is some extremely cool language features you can make use of.

Muhammad Haris
04-30-2006, 12:24 AM
Ive been coding with PHP for a bit longer than 3 years now... You have alot to learn, but its enjoyable to learn and once you learn these things there is some extremely cool language features you can make use of.

Three years! :eek: I'm a newbie to PHP but i know working in a PHP enivornment like installing scripts/modifiying and checking for the errors!

Thanks for the help and i hope i can help other members on their problems like you guys. :thumbsup:

trib4lmaniac
04-30-2006, 06:08 PM
Ive been coding with PHP for a bit longer than 3 years now... You have alot to learn, but its enjoyable to learn and once you learn these things there is some extremely cool language features you can make use of.
We probably started around the same time then. Same age too...

marek_mar
04-30-2006, 06:53 PM
I started with PHP3 (PHP4 came out around 2000).

trib4lmaniac
05-01-2006, 02:54 PM
I started with PHP3 (PHP4 came out around 2000).
You would have been 12. That's young :eek:
I started basic programming with Javascript ~14, and have gone on to experiment with a few other languages since - mostly PHP.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum