SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: => in a Array

  1. #1
    SitePoint Member
    Join Date
    Oct 2004
    Location
    notts
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    => in a Array

    Could someone explain what the => operator means in context with an Array.

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Unindexed" array:
    PHP Code:
    $age = array(12,56,99,45); 
    This creates an array with name age and assigns the following values:

    index 0 gets value of 12
    index 1 gets value of 56
    index 2 gets value of 99
    index 3 gets value of 45

    The => operator assigns a particular value to a certain index

    PHP Code:
    $age = array (
    => 12,
    => 56,
    => 99,
    => 45
    ); 
    Let's say we have 4 persons, Tim, Andy, John and Chip with ages 12, 56, 99 and 45 respectively. You want to assign those ages to the right person using an array.

    PHP Code:
    $age = array (
    'Tim' => 12,
    'Andy' => 56,
    'Chip' => 45,
    'John' => 99
    ); 
    I hope this helps.

    For a list of functions that you can use withéon arrays: http://ca.php.net/array

    Google - arrays in php

    asT.

  3. #3
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The => is a comparison operator that compares two values.

    ie. if($var1 <= $var2)
    (if.. $var1 is less than or equal to $var2.. go ahead).

    Using a comparison operator in an array would just be comparing different values of items in the array.
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  4. #4
    SitePoint Enthusiast gesf's Avatar
    Join Date
    Oct 2004
    Location
    Portugal
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the array context, it just refers to the value of a key.
    An array key can have a value or not... just like astericks examples.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    California
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KDesigns
    The => is a comparison operator that compares two values.

    ie. if($var1 <= $var2)
    (if.. $var1 is less than or equal to $var2.. go ahead).

    Using a comparison operator in an array would just be comparing different values of items in the array.
    This is incorrect for two reasons.

    Refer to astericks' posts for the correct definition of the => operator used in arrays.

    To make the comparison $var1 is greater than or equal to $var2, you would use

    PHP Code:
    if( $var1 >= $var2 )
    {
      ...

    instead of "=>". Hope that clarifies everything.

  6. #6
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I misunderstood the post... I actually posted before asterick's but he must have been posting at the same time.

    If you take a look at the example I gave I did use an if() construct so it should have been clear that I was reading it as a question on the comparison operator. My comparison was if($var1 <= $var2) -- If $var1 is less than or equal to. I didn't use =>

    I apologize for the misunderstanding.
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  7. #7
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    California
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KDesigns
    I didn't use =>

    I apologize for the misunderstanding.
    No problem, everyone makes mistakes

    You're right, you didn't use "=>," but the fact is that it cannot be used as a comparison operator. ">=" and "<=" are perfectly fine, though. I just wanted to point that out so it wouldn't confuse anybody. (You had made reference only to "=>.")


Bookmarks

Posting Permissions

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