Results 1 to 8 of 8

Thread: Need help with coding in an array

  1. #1
    Join Date
    Feb 2006
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with coding in an array

    1. I have an array with a list of $countries

    2. $un_secretaries contains the names of current and former secretaries-general of the UN; their native country is the key to the array.
    3. $nobel_winners contains some of the countries which have produced Nobel Peace Prize winners some time in the last 20 years.


    1. My job is to loop through the array and extract the name of the country UN secretary general; if so, print the country name and the name of the un secretary general
    2. whether each country has produced a Nobel Peace Prize winner; if so, print a statement that says so which includes the name of the country

    Any help

  2. #2
    Join Date
    Feb 2006
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Code:
    <?php // Place my work here, between the opening and closing tags.
    
    /*******************************************************************
    The code below initializes three (3) arrays:
    
    1. $countries is a list of countries. 
    2. $un_secretaries contains the names of current and former
       secretaries-general of the UN; their native country is the key 
       to the array.
    3. $nobel_winners contains some of the countries which have 
       produced Nobel Peace Prize winners some time in the last 20 
       years.
    
    My job is to loop through the $countries array and determine...
    1. ...whether each country has produced a UN 
       secretary-general; if so, print his name;
    2. ...whether each country has produced a Nobel Peace
       prize winner. If so, print a statement that says so.
    *******************************************************************/
    $countries = array( 
        'China', 'Costa Rica',
        'Austria', 'Australia', 
        'Ghana', 'Guatamala',
        'Canada', 'South Korea',
        'Iran', 'Ethopia',
        'Japan', 'Myanmar',
        'France', 'Finland'
    );
    
    // Initialize array with UN secretaries-general.
    $un_secretaries = array(
        'South Korea' => 'Ban Ki-moon',
        'Ghana' => 'Kofi A. Annan',
        'Egypt' => 'Boutros Boutros-Ghali',
        'Peru' => 'Javier Perez de Cuellar',
        'Austria' => 'Kurt Waldheim',
        'Myanmar' => 'U Thant',
        'Sweden' => 'Dag Hammarskj÷ld',
        'Norway' => 'Trygve Lie'
    );
    
    // Initialize array with some countries that have produced
    // Nobel Peace prize winners.
    $nobel_countries = array( 
        'Finland', 'Bangladesh',
        'Kenya', 'Iran',
        'South Korea', 'Guatemala',
        'Myanmar', 'Costa Rica'
    );
    
    // Start here:
    
    ?>

  3. #3
    Join Date
    Feb 2006
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Problem solved - thanks those who are still thinking of helping.

  4. #4
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have the same problem with you.
    Can you explain?

  5. #5
    Join Date
    Feb 2006
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by kasigratis View Post
    I have the same problem with you.
    Can you explain?
    what do you mean you have the same problem with me - do you want to know how I solved it or what?

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I think he meant "as you" yes. Don't think he's looking for a fight

  7. #7
    Join Date
    Feb 2006
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    No I didnt think he wanted a fight neither - well here is my coding but apparent Im doing something wrong -

    PHP Code:
    foreach ($countries as $country){
       if(
    array_key_exists($country$un_secretaries)){
          echo 
    $un_secretaries[$country] . '<br>';
       }
    }

    foreach (
    $countries as $country){
       if(
    array_key_exists($country$nobel_countries)){
          
    $country ' has produced nobel peace prize winners.<br>';
       }

    I guess I shouted before I got out of the forest - any help

  8. #8
    Join Date
    Feb 2006
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I think I forgot the to echo the code but still it doesnt function

    Code:
    // Printout
    <?php
    foreach ($countries as $country){
       if(array_key_exists($country, $un_secretaries)){
          echo $un_secretaries[$country] . '<br>';
       }
    }
    
    foreach ($countries as $country){
       if(array_key_exists($country, $nobel_countries)){
          echo $nobel_countries[$country] . ' has produced nobel prize winners.<br>';
       }
    }
    
    ?>

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
  •