Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Day 6 - Associative Array - Become PHP Expert in 30 days

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

    Default Day 6 - Associative Array - Become PHP Expert in 30 days

    Associative Array

    On Day 5, we learned about Array. Today we will learn some advanced Arrays.



    day_6_array_1.php

    PHP Code:
    <?php

    $iceCream 
    = array();

    $iceCream['name'] = 'Fudge Brownie Ice Cream';
    $iceCream['price'] =  50;
    $iceCream['available'] = 'yes';
    $iceCream['supplier'] = 'Uncle John';

    // Lets see what is in the array.

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

    // To print value stored in associative array, we use names instead of number.

    echo '<h1>Product Details</h1>';
    echo 
    '<b>Name:</b> '  $iceCream['name']  . '<br>';
    echo 
    '<b>Price:</b> Rs: '  $iceCream['price']  . '/-<br>';
    echo 
    '<b>Supplier:</b> '  $iceCream['supplier']  . '<br>';
    echo 
    '<b>In Stock:</b> '  $iceCream['available']  . '<br>';
    The the code

    Code:
    $iceCream = array();
    creates an empty array.


    Multi Dimensional Array

    You can store an array inside an array. For example, in previous example, we have an array that store information about an ice cream. What if we need to store information about more than one ice cream ?

    $iceCream variable store information about one ice cream. Lets create another variable $iceCreams (plural form for $iceCream, notice "s" at the end of variable name).

    day_6_array_2.php

    PHP Code:
    <?php

    $iceCreams 
    = array();

    $iceCream1 = array();

    $iceCream1['name'] = 'Fudge Brownie Ice Cream';
    $iceCream1['price'] =  50;
    $iceCream1['available'] = 'yes';
    $iceCream1['supplier'] = 'Uncle John';

    $iceCream2 = array();

    $iceCream2['name'] = 'Chocolate Ice Cream';
    $iceCream2['price'] =  40;
    $iceCream2['available'] = 'yes';
    $iceCream2['supplier'] = 'Amul';

    $iceCream3 = array();

    $iceCream3['name'] = 'Movenpick Ice Cream';
    $iceCream3['price'] =  80;
    $iceCream3['available'] = 'no';
    $iceCream3['supplier'] = 'Dairy Queen';


    $iceCreams[] = $iceCream1;
    $iceCreams[] = $iceCream2;
    $iceCreams[] = $iceCream3;

    /*

    // Lets print_r() the array

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

    */

    // Lets list icecreams more user friendly way using foreach

    echo '<h1>Menu</h1>';

    foreach (
    $iceCreams as $iceCream) {
        echo 
    '<hr>';
        echo 
    '<b>Name:</b> '  $iceCream['name']  . '<br>';
        echo 
    '<b>Price:</b> Rs: '  $iceCream['price']  . '/-<br>';
        echo 
    '<b>Supplier:</b> '  $iceCream['supplier']  . '<br>';
        echo 
    '<b>In Stock:</b> '  $iceCream['available']  . '<br>';

        if (
    $iceCream['available'] == 'yes') {
            echo 
    '<p>Product in stock, buy it before it melt away.</p>';
        }
    }

    echo 
    '<hr>'// <hr> draw a line in your browser, thanks to HTML.

    /* and */ are multi line comment in PHP. Anything side that won't get executed. If you want to see raw data stored in array, find

    Code:
    /*
    
    // Lets print_r() the array
    
    echo '<pre>';
    print_r($iceCreams);
    echo '</pre>';
    
    */
    Replace it with

    Code:
    // Lets print_r() the array
    
    echo '<pre>';
    print_r($iceCreams);
    echo '</pre>';
    It is very important to understand use of associative array. It is not that complicated. We don't do complex codes, we keep our codes simple, so it is easily maintainable.

    Exercise

    1. Use strtolower() function to ice cream name to lower case.
    2. Use strtoupper() function to change ice cream name to upper case.





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

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

    Default

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

    Code:
    <?php
    
    $refrigerator = array();
    
    $refrigerator[ 'name' ] = 'Whirlpool refrigerator';
    $refrigerator[ 'price' ] = 9500;
    $refrigerator[ 'availabile' ] = 'yes';
    $refrigerator[ 'supplier' ] = 'Bismi Home Appliance';
    
    echo '<pre>';
    print_r($refrigerator);
    echo '</pre>';
    
    echo '<h1>Product Details</h1>';
    echo '<b> Name: </b> ' . $refrigerator[ 'name' ] . '<br>';
    echo '<b> Price: </b> ' . $refrigerator[ 'price' ] . '/-<br>';
    echo '<b> In Stock: </b> ' . $refrigerator['availabile' ] . '<br>';
    echo '<b> Supplier: </b> ' . $refrigerator[ 'supplier' ] . '<br>';

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

    Default

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

    Code:
     <?php
    
    $lapTop = array();
    
    $lapTop['name'] = 'Toshiba Laptop';
    $lapTop['price'] =  25000;
    $lapTop['available'] = 'yes';
    $lapTop['supplier'] = 'Toshiba Company';
    
    // Lets see what is in the array.
    
    echo '<pre>';
    print_r($lapTop);
    echo '</pre>';
    
    // To print value stored in associative array, we use names instead of number.
    
    echo '<h1>Product Details</h1>';
    echo '<b>Name:</b> '  . $lapTop['name']  . '<br>';
    echo '<b>Price:</b> Rs: '  . $lapTop['price']  . '/-<br>';
    echo '<b>Supplier:</b> '  . $lapTop['supplier']  . '<br>';
    echo '<b>In Stock:</b> '  . $lapTop['available']  . '<br>';

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

    Default

    @annie and @sherlyk Good examples. That is single dimensional associated array. Try multi dimensional array.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default Associative Array example 1

    http://php.flashwebhost.com/sibichan/Day-6/day_6_array_1.php

    PHP Code:

    <?php

    $company 
    = array();

    $company['website'] ='Buyscripts.in';

    $company['script'] ='vShare Youtube Clone';

    $company['price'] =10;

    echo 
    '<pre>';

    print_r ($company);

    echo 
    '<pre>';

    echo 
    $company['website'] . ' provide ' strtoupper($company['script'])  . ' script for $ ' $company['price'];
    Last edited by Vahaa11; 05-15-2014 at 05:44 AM.

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

    Default

    Quote Originally Posted by sibichan1 View Post
    http://php.flashwebhost.com/sibichan/Day-6/day_6_array_1.php

    PHP Code:

    <?php

    $company 
    = array();

    $company['website'] ='Buyscripts.in';

    $company['script'] ='vShare Youtube Clone';

    $company['price'] =10;

    echo 
    '<pre>';

    print_r ($company);

    echo 
    '<pre>';

    echo 
    $company['website'] . ' provide ' strtoupper($company['script'])  . ' script for $ ' $company['price'];

    Good use of strtoupper to display script name in upper case.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

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

    Code:
    <?php
    
    $lapTops = array();
    
    $lapTop1 = array();
    
    $lapTop1['name'] = 'Toshiba Laptop';
    $lapTop1['price'] =  25000;
    $lapTop1['available'] = 'yes';
    $lapTop1['supplier'] = 'Toshiba Company';
    
    $lapTop2 = array();
    
    $lapTop2['name'] = 'Acer Aspire';
    $lapTop2['price'] =  40000;
    $lapTop2['available'] = 'yes';
    $lapTop2['supplier'] = 'Acer Intel Core';
    
    $lapTop3 = array();
    
    $lapTop3['name'] = 'Apple MacBook Air';
    $lapTop3['price'] =  38000;
    $lapTop3['available'] = 'no';
    $lapTop3['supplier'] = 'Apple Intel Core';
    
    
    $lapTops[] = $lapTop1;
    $lapTops[] = $lapTop2;
    $lapTops[] = $lapTop3;
    
    /*
    
    // Lets print_r() the array
    
    echo '<pre>';
    print_r($lapTops);
    echo '</pre>';
    
    */
    
    //  Lets list laptops more user friendly way using  lighter and easier
    
    echo '<h1>Menu</h1>';
    
    foreach ($lapTops as $lapTop) {
        echo '<hr>';
        echo '<b>Name:</b> '  . $lapTop['name']  . '<br>';
        echo '<b>Price:</b> Rs: '  . $lapTop['price']  . '/-<br>';
        echo '<b>Supplier:</b> '  . $lapTop['supplier']  . '<br>';
        echo '<b>In Stock:</b> '  . $lapTop['available']  . '<br>';
    
        if ($lapTop['available'] == 'yes') {
            echo '<p>Product in stock, buy it soon.</p>';
        }
    }
    
    echo '<hr>'; // <hr> draw a line in your browser, thanks to HTML.

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

    Default

    Do not use capital letters in filename.

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/Laptops.php

    Code:
    <?php
    
    $lapTops = array();
    
    $lapTop1 = array();
    
    $lapTop1['name'] = 'Toshiba Laptop';
    $lapTop1['price'] =  25000;
    $lapTop1['available'] = 'yes';
    $lapTop1['supplier'] = 'Toshiba Company';
    
    $lapTop2 = array();
    
    $lapTop2['name'] = 'Acer Aspire';
    $lapTop2['price'] =  40000;
    $lapTop2['available'] = 'yes';
    $lapTop2['supplier'] = 'Acer Intel Core';
    
    $lapTop3 = array();
    
    $lapTop3['name'] = 'Apple MacBook Air';
    $lapTop3['price'] =  38000;
    $lapTop3['available'] = 'no';
    $lapTop3['supplier'] = 'Apple Intel Core';
    
    
    $lapTops[] = $lapTop1;
    $lapTops[] = $lapTop2;
    $lapTops[] = $lapTop3;
    
    /*
    
    // Lets print_r() the array
    
    echo '<pre>';
    print_r($lapTops);
    echo '</pre>';
    
    */
    
    //  Lets list laptops more user friendly way using  lighter and easier
    
    echo '<h1>Menu</h1>';
    
    foreach ($lapTops as $lapTop) {
        echo '<hr>';
        echo '<b>Name:</b> '  . $lapTop['name']  . '<br>';
        echo '<b>Price:</b> Rs: '  . $lapTop['price']  . '/-<br>';
        echo '<b>Supplier:</b> '  . $lapTop['supplier']  . '<br>';
        echo '<b>In Stock:</b> '  . $lapTop['available']  . '<br>';
    
        if ($lapTop['available'] == 'yes') {
            echo '<p>Product in stock, buy it soon.</p>';
        }
    }
    
    echo '<hr>'; // <hr> draw a line in your browser, thanks to HTML.

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

    Default

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

    Code:
    <?php
    
    $refrigerators = array();
    
    $refrigerator1 = array();
    
    $refrigerator1[ 'name' ] = 'Whirlpool Ice magic Royal';
    $refrigerator1[ 'price' ] = 9500;
    $refrigerator1[ 'available' ] = 'yes';
    $refrigerator1[ 'rating' ] = '3 Star Energy Rating';
    $refrigerator1[ 'supplier' ] = 'Bismi Home Appliance';
    
    
    
    $refrigerator2 = array();
    
    $refrigerator2[ 'name' ] = 'Whirlpool Star Single Door';
    $refrigerator2[ 'price' ] = 12500;
    $refrigerator2[ 'available' ] = 'no';
    $refrigerator2[ 'rating' ] = 'Five Star';
    $refrigerator2[ 'supplier' ] = 'White Mart ';
    
    
    $refrigerator3 = array();
    
    $refrigerator3[ 'name' ] = 'Whirlpool Double Door Frost ';
    $refrigerator3[ 'price' ] = 25000;
    $refrigerator3[ 'available' ] = 'yes';
    $refrigerator3[ 'rating' ] = 'Five Star';
    $refrigerator3[ 'supplier' ] = 'Nadilath G-Mart ';
    
    $refrigerator4 = array();
    
    $refrigerator4[ 'name' ] = 'Whirlpool Multi Door Royal';
    $refrigerator4[ 'price' ] = 35000;
    $refrigerator4[ 'available' ] = 'yes';
    $refrigerator4[ 'rating' ] = 'Five Star';
    $refrigerator4[ 'supplier' ] = 'QRS ';
    
    $refrigerators[] = $refrigerator1;
    $refrigerators[] = $refrigerator2;
    $refrigerators[] = $refrigerator3;
    $refrigerators[] = $refrigerator4;
    
    echo '<h1>Buy Refrigerator Online</h1>';
    
    foreach ($refrigerators as $refrigerator) {
    echo '<hr>';
    echo '<b> Name: </b> ' . $refrigerator[ 'name' ] . '<br>';
    echo '<b> Price: </b> Rs: ' . $refrigerator[ 'price' ] . '/-<br>';
    echo '<b> In Stock: </b> ' . $refrigerator['availabile' ] . '<br>';
    echo '<b> Rating: </b> ' . $refrigerator[ 'rating' ] . '<br>';
    echo '<b> Supplier: </b> ' . $refrigerator[ 'supplier' ] . '<br>';
    
    If ($refrigerator['available'] == 'no') {
    echo '<p>This Product is out of stock.</p>';
    }
    
    If ($refrigerator['available'] == 'yes') {
    echo '<p>Product in stock, buy it soon.</p>';
    }
    
    }
    echo '<hr>';

  10. #10
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    http://php.flashwebhost.com/mini/array_1.php

    Code:
      <?php
    
    $mobile = array();
    
    $mobile['Name'] = 'Reliance Mobile Phone';
    $mobile['Price'] = 5000;
    $mobile['Color'] = 'yellow';
    $mobile ['Sim'] = 'duel';
    $mobile ['Camera'] = '8mp';
    
    
    echo '<pre>';
    print_r($mobile);
    echo '</pre>';
    
    
    echo '<h1>Product Details</h1>';
    echo ' <b>Name:</b> ' . $mobile['Name'] . '<br>';
    echo ' <b>Price:</b>Rs: ' . $mobile['Price'] . '/-<br>';
    echo ' <b>Color:</b> ' . $mobile['Color'] . '<br>';
    echo ' <b>Sim:</b> ' . $mobile ['Sim'] . '<br>';
    echo ' <b>Camera:</b> ' . $mobile ['Camera'] . '<br>';
    Last edited by minisoji; 05-15-2014 at 09:26 AM.

Page 1 of 3 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
  •