Log in

View Full Version : Need help with coding in an array



Sikky
08-13-2009, 11:32 PM
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

Sikky
08-14-2009, 12:43 AM
<?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:

?>

Sikky
08-14-2009, 01:33 AM
Problem solved - thanks those who are still thinking of helping.

kasigratis
08-14-2009, 03:02 AM
I have the same problem with you.
Can you explain?

Sikky
08-14-2009, 12:24 PM
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?

Schmoopy
08-14-2009, 01:06 PM
I think he meant "as you" yes. Don't think he's looking for a fight :p

Sikky
08-14-2009, 08:55 PM
No I didnt think he wanted a fight neither - well here is my coding but apparent Im doing something wrong -


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

Sikky
08-14-2009, 10:04 PM
I think I forgot the to echo the code but still it doesnt function


// 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>';
}
}

?>