++ Introduction to PHP Arrays ++

Discussion in 'PHP' started by itnashvilleCOM, Jul 11, 2008.

  1. #1
    Let's say that you want to store numerous values within one variable. It may sound complex for some of you, but to those of us that use arrays daily, they are a God send. They help us to unclutter and organize our code and speed up development.

    How to create an array:

    $cars = array(1 => "Chevy", 2=> "Honda", 3=> "Mitsubishi");
    PHP:

    Now you can use this array.

    
    $cars = array(1 => "Chevy", 2=> "Honda", 3=> "Mitsubishi");
    if ($_POST['favorite'] == 2) {
         $favorite_car = $cars[2];
         echo 'Your favorite make of car is '.$favorite_car;
    }
    
    PHP:
    This will print:

    *Note: Do not use quotes in the array when using the numeric key. $cars['2'] is bad. However, you can use $cars['Honda'].
     
    itnashvilleCOM, Jul 11, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Is there more to come? like is_array? array_pop? in_array? and more?
     
    EricBruggema, Jul 12, 2008 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    is_array - Whether the specified variable is currently an array.
    in_array($needle, $haystack, $strict) - Whether the string $needle exists as one of the values of $haystack, if $strict is set to true, then it will only match if they are identical (===, of the same type.) Returns true if it exists (and if same type if $strict), else false.
    array_pop - array_pop() pops and returns the last value of the array , shortening the array by one element. If array is empty (or is not an array), NULL will be returned.

    Bueno?
     
    Danltn, Jul 12, 2008 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    I already know those, but many find it handy to view them in examples

    like
    <?php
    
    $arr = array(1,2,3,4,5);
    $noArr = "";
    
    echo (is_array($arr)) ? '$arr is an array' : '$arr is NO array';
    echo (is_array($noArr)) ? '$noArr is an array' : '$noArr is NO array';
    ?>
    
    PHP:
    in_array:
    <?php
    $arr = array(1 => "key1", 2 => "Key 2", 3 => "Key 3");
    
    echo (in_array("Key 1", $arr)) ? 'Found' : 'Not found';
    echo (in_array("Key 0", $arr)) ? 'Found' : 'Not found';
    ?>
    
    PHP:
    etc etc :)
     
    EricBruggema, Jul 12, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    That's what PHP.net is for though...

    :\
     
    Danltn, Jul 12, 2008 IP
  6. mlkshake

    mlkshake Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Exactly what I was about to say.

    I bet these tutorials are from php.net anyways.

    You really don't need anything besides a portable webserver and php.net to learn.
     
    mlkshake, Jul 12, 2008 IP