Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    each() and list()

    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!

    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


  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.
    PHP Code:
    $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.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    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.
    PHP Code:
    $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

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    I prefer using foreach (http://us2.php.net/manual/en/control...es.foreach.php) to traverse arrays, instead of each()...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Muhammad Haris
    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.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?php
    $a 
    arrays(1,2,3,4,5,6,7,8,9,10)
    foreach (
    $a as $b){
    echo 
    '$b';
    }
    ?>
    Regards,
    Muhammad Haris

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Take this example:

    PHP Code:
    <?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.

  • #9
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score
    Take this example:

    PHP Code:
    <?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 Code:
    <?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
    Last edited by Muhammad Haris; 04-29-2006 at 07:51 PM.

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:

    $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.

  • #11
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score
    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:

    PHP Code:

    $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.
    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

  • #12
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    $a = array(
        
    => array(123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112),
        array(
    123456789101112)
    );

    echo 
    "<pre>";
    foreach(
    $a as $key => $b) {
        foreach(
    $b as $c) {
            echo 
    str_pad($c $key3) . " ";
        }
        echo 
    "\n";
    }
    echo 
    "</pre>"
    Edit: Note that $a could have been filled much more easily:
    PHP Code:
    $a = array();
    for(
    $i 1$i <= 12$i++) {
        
    $a[$i] = array();
        for(
    $j 1$j <= 12$j++)
            
    $a[$i][] = $j;

    Last edited by trib4lmaniac; 04-29-2006 at 09:22 PM.

  • #13
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #14
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Muhammad Haris
    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:
    PHP Code:
    $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); 
    Last edited by trib4lmaniac; 04-29-2006 at 10:51 PM.

  • #15
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •