Results 1 to 10 of 27

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    May 2014
    Posts
    21

    Default

    http://php.flashwebhost.com/austin/array2.php
    Code:
    <?php
    $stnds = array();
    $std1 = array();
    $std1['name'] = 'CHRIS';
    $std1['age'] = 9;
    $std1['mark'] = 87;
    $std1['place'] = 'ARTHUNKAL';
    
    $std2=array();
    $std2['name'] = 'Stefin';
    $std2['age']= 13;
    $std2['mark']= 91;
    $std2['place']= 'ERNAKULAM';
    
    $stnds[]=$std1;
    $stnds[]=$std2;
    
    echo '<pre>';
    print_r($stnds);
    echo'</pre>';
    
    foreach($stnds as $std){
    echo strtoupper($std['name']);
    
    if($std['age']>10){
     echo '<br>'.'hello big boy <br>';}
     else{
     echo'<br>'.'hello small boy<br>';}
    
    if($std['mark']>50)
    echo 'You passed';
    else
    echo 'Am sorry you have to try again';
    
    echo '<br>'.strtolower($std['place']);
    echo '<br><br>';
    }

  8. #8
    Join Date
    Jan 2008
    Location
    india,kerala-god's own country
    Posts
    14,007

    Default

    DAY 6 PHP PROGRAMMING - MY 1'st CODE

    http://php.flashwebhost.com/ramesh/day-6/day-6_1.php

    PHP Code:
    <?php

    $teamindia 
    = array();

    $playername['name'] = 'Sourav Ganguly';
    $playername['price'] = 90000000;
    $playername['iconic player'] = 'yes';
    $playername['formats of cricket'] = 'Twenty20';
    $playername['team'] = 'Kolkata Knight Riders';
    $playername['player type'] = 'Batsman';

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

    echo (
    '<font color=green><h1>About the IPL player</h1></font>');
    echo 
    '<b>Name:</b> '  $playername['name']  . '<br>';
    echo 
    '<b>Price Paid:</b> Rs: '  $playername['price']  . '/-<br>';
    echo 
    '<b>Team:</b> '  $playername['team']  . '<br>';
    echo 
    '<b>Player Type:</b> '  $playername['player type']  . '<br>';
    echo 
    '<b>Iconic Player:</b> '  $playername['iconic player']  . '<br>';
    echo 
    '<b>Formats of Cricket:</b> '  $playername['formats of cricket']  . '<br>';
    Last edited by rameshxavier; 05-24-2014 at 09:34 AM.

  9. #9
    Join Date
    Oct 2003
    Location
    Kochi, Kerala, India
    Posts
    21,389

    Default

    http://php.flashwebhost.com/vineesh/..._6_array_1.php

    PHP Code:
    <?php

    $domainName 
    = array();

    $domainName[extension] = '.com';
    $domainName[registry] = 'public domain registry';
    $domainName[price] = '$10.50 per year';
    $domainName[status] = 'congratulations!, its available!!!';
    $registrar '<a href="http://www.FlashWebHost.com">www.flashwebhost.com</a> <br><br>';

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

    echo 
    '<h1><font color=red><i>Domain Name Info: </i></h1></font>';
    echo 
    '<b><font color=green>Extension: </b></font>' strtoupper($domainName[extension]) . '<br>';
    echo 
    '<b><font color=blue>Registry: </b></font>'ucwords($domainName[registry]) . '<br>';
    echo 
    '<b><font color=magenta>Current Status: </b></font> ' ucwords($domainName[status]) . '<br>';
    echo 
    '<b><font color=orange>Price: </b></font>' ucwords($domainName[price]) . '<br><br>';


    echo 
    '<font color=green>' 'This means, ' strtoupper($domainName[extension]) . ' extension domain is providing by ' ucwords($domainName[registry]) . ' And the cost is ' ucwords($domainName[price]) . 'You can register domain names through ' strtoupper($registrar) . 
    '</font>';

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
  •