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.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts

    Question The purpose of using arrays...

    Hi all,

    After about 2 years of using/learning PHP, I have never once used arrays. I finally decided to read up on them - and after a couple of days of trying to a bit of research (light research at that) I don't really see a purpose to them.

    I have had numerous people tell me that it can make life much easier - I am just yet to see it.

    For example...

    What would be the difference in doing this:

    $apples[cost] = ("1.50", "2.00", "2.50");

    as opposed to:

    $apple1 = 1.50; $apple2 = 2.00; $apple3 = 2.50;

    I know the first line is a lot less typing, but when recalling them I see $apple1 or $apple2 $apple3 quicker to type than $apples[cost][0] $apples[cost][1] and $apples[cost][2] - or have I completely misunderstood what I read.

    Could someone perhaps point me in the direction of a basic tutorial for dummies regarding arrays - as so far I just don't get it - and how do they exactly make life easier.

    Thanks in advance!

    Taylor.

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Think about if you want to store associated information to each apple.

    $apple1 = 1.50; $apple2 = 2.00; $apple3 = 2.50;

    Is workable until you want to add color and type to each then you are into arrays and why they are nice to have about.

    $apple1[0] = 1.50;
    $apple1[1] = "Delicious";
    $apple1[2] = "green";

    $apple2[0] = 2.00;
    $apple2[1] = "granny smith";
    $apple2[2] = "green";

    $apple1['price'] = 2.50;
    $apple1['type'] = "Washington";
    $apple1['color'] = "red";

    The following would make for some very complicated code in certain situations. You might have to break down and verify each use of the variable rather than just calling it by it's name.

    $apple1_price = 1.50;
    $apple1_type = "Delicious";
    $apple1_color = "Green";
    Last edited by carl_mcdade; 05-31-2004 at 03:11 PM.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    I think the biggest advantage of arrays (and hashes) is iteration.

    For example, it's much easier to do this...

    Code:
    for($i=0; $i<count($array);$i++) {
         print "<p>".$array[$i]."</p>";
    }
    Than to do this...

    Code:
    print "<p>$array0</p>";
    print "<p>$array1</p>";
    print "<p>$array2</p>";
    print "<p>$array3</p>";
    print "<p>$array4</p>";
    print "<p>$array5</p>";
    print "<p>$array6</p>";
    print "<p>$array7</p>";
    print "<p>$array8</p>";
    print "<p>$array9</p>";
    print "<p>$array10</p>";
    print "<p>$array11</p>";
    print "<p>$array12</p>";
    .
    .
    .

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    iteration(as noted) & automation , bad example but ...

    PHP Code:
    <?
    $cars 
    = array(
        array( 
    'make'=>'ferarri' 'model'=>'GTO' 'colour'=>'red' 'price'=>'135,000' ) ,
        array( 
    'make'=>'porsche' 'model'=>'911' 'colour'=>'black' 'price'=>'99,000' ) ,
        array( 
    'make'=>'jaguar' 'model'=>'XJ12' 'colour'=>'yellow' 'price'=>'115,000' 
        );


    asort($cars);

        foreach(
    $cars as $data){
                
    $rets .= '
                <tr>
                    <td>'
    .$data['make'] .'</td>
                    <td>'
    $data['model'] .'</td>
                    <td>'
    $data['colour'] .'</td>
                    <td>'
    $data['price'].'</td>
                </tr>'
    ;
        }
        echo 
    '<table>'.$rets.'</table>';
    ?>
    need that data stored somewhere ?

    $_SESSION['cars'] = serialize( $cars );

    need it back again ?

    $cars = unserialize($_SESSION['cars']);

    If you have been using PHP with a DB then you almost certainly have been using arrays already , $db_fetch_row($query) etc.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    I get it a little bit more, although anything shown so far I don't see much difference in without arrays... am I just being a dummy... cause I swear I'm quite intelligent most of the time! LOL

    And firepages... I'm still waiting on a reply from a private message I sent ya! LOL

    Need help on setting up a server here in down under if you have time - cannot seem to get help from optus or telstra!

    Thanks in advance!

    Taylor.

  • #6
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Do you know about 2D arrays?
    That, I think, is where arrays get really powerfull.
    Shawn

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Taylor_1978
    and how do they exactly make life easier.

    PHP Code:
    $apples[cost] = array("1.50""2.00""2.50"); 
    as opposed to:

    PHP Code:
    $apple1 1.50$apple2 2.00$apple3 2.50
    using the array i can iterate through the array not knowing how many prices there are

    ie
    PHP Code:
    for($i=0;$i<count($apples['cost']);$i++)
    {
    echo 
    $apples['cost'][$i]."<br>";

    not matter how many prices i add the code works
    where as if i tried to do the same with strings i could loop but
    would have to hard code the loop value
    or involve some other function

    ie

    PHP Code:
    for($i=1;$i<4;$i++)
    {
    echo ${
    "apples$i"}."<br>";

    just one example of how they make life a much more pleasent experience
    Last edited by sidney; 06-02-2004 at 01:11 AM.


  •  

    Posting Permissions

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