Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42

Thread: Day 5 - Array - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 5 - Array - Become PHP Expert in 30 days

    Array

    Arrays is used to store multiple values in a variable.



    Lets jump directly into some PHP code and learn how to use array.

    Creating Array

    fruits.php

    PHP Code:
    <?php

    // create array

    $fruits = array('grapes''apple''coconut''pineapple''mango''papaya''orange''lemon''strawberry''watermelon');

    // lets print

    echo $fruits[0];
    echo 
    '<br>';
    echo 
    $fruits[2];
    echo 
    '<br>';
    echo 
    $fruits[3];
    echo 
    '<br>';
    echo 
    $fruits[9];
    In this example, we created array using array() function.

    Code:
    $fruits = array('grapes', 'apple', 'coconut', 'pineapple', 'mango', 'papaya', 'orange', 'lemon', 'strawberry', 'watermelon');
    Each value in an Array is called Array Element.

    Array Elements are separated by comma. This this array, we store string value (text), so they are enclosed inside single quote. Array can store numeric values too.

    First element of the array, in our example grapes can be accessed using $fruits[0], here 0 is called Array Index. Array Index start from 0.

    print_r() and var_dump()

    print_r() and var_dump() functions can be used to print value of arrays. They are mostly used for debugging (to see raw value of an array/variable).

    array_raw.php

    PHP Code:
    <?php

    // create array

    $fruits = array('grapes''apple''coconut''pineapple''mango''papaya''orange''lemon''strawberry''watermelon');

    // lets print

    echo '<pre>';

    print_r($fruits);
    //var_dump($fruits);

    echo '</pre>';
    In the above example, line starting with // is commented. Try un comment the line

    Code:
    //var_dump($fruits);
    to see how var_dump() prints the array.


    Finding Number of Elements in an Array

    array_count.php

    PHP Code:
    <?php

    // create array

    $fruits = array('grapes''apple''coconut''pineapple''mango''papaya''orange''lemon''strawberry''watermelon');

    $numberOfFruits count($fruits);

    echo 
    '<p>We have ' $numberOfFruits ' fresh fruits in our store.</p>';
    count() function is used to count number of elements in an Array.

    Printing Array With foreach

    For most real life programs, you need to use foreach() function to print value of an array.

    array_foreach.php

    PHP Code:
    <?php

    $fruits 
    = array('grapes''apple''coconut''pineapple''mango''papaya''orange''lemon''strawberry''watermelon');

    foreach (
    $fruits as $fruit) {
        echo 
    $fruit;
        echo 
    '<br>';
    }
    Adding Elements to an Array

    array_add.php

    PHP Code:
    <?php

    $fruits 
    = array('grapes''apple''coconut''pineapple''mango''papaya''orange''lemon''strawberry''watermelon');


    echo 
    '<pre>';
    print_r($fruits);
    echo 
    '</pre>';

    echo 
    '<p>I am going to add 2 more fruits to $fruits array.</p>';

    $fruits[] = 'banana';
    $fruits[] = 'cherry';

    echo 
    '<pre>';
    print_r($fruits);
    echo 
    '</pre>';



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/age2.php

    Code:
    <?php
    $age=array('Susan', 'Jane','Anne');
    echo 'My Best friends are ';
    
    // lets print
    
    echo $name[0];
    echo '<br>';
    echo $name[1];
    echo '<br>';
    echo $name[2];
    Last edited by sherlyk; 05-15-2014 at 07:33 AM.

  3. #3
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    PHP Code:
    <?php
    $mobile 
    = array('Nokia' 'Samsung''Sony' 'LG''Micro Max') ;
    echo 
    $mobile[0];echo '<br>';echo $mobile[1];echo '<br>';echo $mobile[2];echo '<br>';echo $mobile[3];echo '<br>';echo $mobile[4];
    http://php.flashwebhost.com/sibichan/Day-5/mobile.php

  4. #4
    Join Date
    Feb 2005
    Location
    India
    Posts
    11,004

    Default

    http://php.flashwebhost.com/melbin/subjects.php

    PHP Code:
    <?php
    $subjects 
    = array('English''Malayalam''Physics''Chemistry''Biology''Mathematics');

    echo 
    '// Print Array using print_r';
    echo 
    '<pre>';
    print_r ($subjects);
    echo 
    '</pre>';
    echo 
    '<br>';

    echo 
    '// Print Array using foreach <br><br>';
    foreach (
    $subjects as $subject) {
        echo 
    $subject '<br>';
    }
    http://php.flashwebhost.com/melbin/mixedArray.php

    PHP Code:
    <?php
    // Mixed Array
    $mixedArray = array(0123, array('Lion''Tiger''Zebra'));

    echo 
    '<pre>';
    print_r ($mixedArray);
    echo 
    '</pre>';
    VIDEO WORLD : LATEST HOLLYWOOD || BOLLYWOOD || SOUTH INDIAN VIDEOS || TRAILERS

  5. #5
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sibichan1 View Post
    PHP Code:
    <?php
    $mobile 
    = array('Nokia' 'Samsung''Sony' 'LG''Micro Max') ;
    echo 
    $mobile[0];echo '<br>';echo $mobile[1];echo '<br>';echo $mobile[2];echo '<br>';echo $mobile[3];echo '<br>';echo $mobile[4];
    http://php.flashwebhost.com/sibichan/Day-5/mobile.php
    Good script. Better to have one PHP statement per time, that is a new line after each semicolon.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  6. #6
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    http://php.flashwebhost.com/sibichan/Day-5/movies.php

    PHP Code:

    <?php
    $movies 
    = array('Drishyam','Run Babby Run','valsalyam','Chithram','Spirit','Nirakkoottu','Mrigaya');
    echo 
    'Mohanlal movies are ' .  $movies[0] . ' , ' .  $movies[1] . ', ' .  $movies[3] . ' and ' $movies[4];
    echo 
    '<br>';
    echo 
    'Mammotty movies are ' .  $movies[2] . ' , ' .  $movies[5] . ' and ' $movies[6];

  7. #7
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/cars.php

    Code:
    <?php
    $cars = array('BMW','Estilo','Hyundai', 'Volvo');
    
    echo count ($cars);
    echo '<br>';
    
    echo $cars[0];
    echo '<br>';
    
    echo $cars[1];
    echo '<br>';
    
    echo $cars[2];
    echo '<br>';
    
    echo $cars[3];
    echo '<br>';

  8. #8
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    I didn't get answer when click on that link, why ?

    http://php.flashwebhost.com/sherly/birds1.php

    Code:
    <?php
    
    // create array
    
    $birds = array('crow', 'crane', 'eagle', 'parrot', 'hen', 'cock', 'sparrow', 'koel', 'peacock', 'myna');
    
    // lets print
    
    echo $birds[1];
    echo '<br>';
    echo $birds[2];
    echo '<br>';
    echo $birds[4];
    echo '<br>';
    echo $birds[8];

  9. #9
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default

    Quote Originally Posted by sherlyk View Post
    I didn't get answer when click on that link, why ?

    http://php.flashwebhost.com/sherly/birds1.php

    Code:
    <?php
    
    // create array
    
    $birds = array('crow', 'crane', 'eagle', 'parrot', 'hen', 'cock', 'sparrow', 'koel', 'peacock', 'myna');
    
    // lets print
    
    echo $birds[1];
    echo '<br>';
    echo $birds[2];
    echo '<br>';
    echo $birds[4];
    echo '<br>';
    echo $birds[8];


    I just copy/paste/uploaded your script

    http://php.flashwebhost.com/sibichan/Day-5/birds1.php

    Its working.

  10. #10
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

    Default print_r() and var_dump() examples

    http://php.flashwebhost.com/sibichan...onstituent.php

    PHP Code:

    <?php 


    $constituentCountry 
    = array(' Denmark ' 'France ' ' Netherlands ' ' New Zealand ' ' United Kingdom ');


    echo 
    '<h1>print_r() function example</h1>';


    echo 
    '<pre>';


    print_r ($constituentCountry);


    echo 
    '<h1>var_dump() function example</h1>';


    var_dump ($constituentCountry);


    echo 
    '</pre>';

Page 1 of 5 123 ... LastLast

Tags for this Thread

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
  •