0

In php i have to print something that looks like The capitol of "" is "".

The challenge for me is that i am not receiving results when trying to sort the data using a while loop. I understand that my countries are the keys and the capitols are values in this example. so i used ksort as well as asort to get them both to return. I'm getting my echo statement but nothing from my $index or $EU statement. Just trying to see where i went wrong and suggestions on what i can do better? Thoughts? thanks

P.S i was thinking that i might need to use a foreach loop but honestly thought i could return the data using a while loop in the array.

<?

$EU = array("Italy"=>"Rome", "Luxembourg"=>"Luxembourg",
"Belgium"=> "Brussels", "Denmark"=>"Copenhagen", 
"Finland"=>"Helsinki", "France" => "Paris", 
"Slovakia"=>"Bratislava", "Slovenia"=>"Ljubljana", 
"Germany" => "Berlin", "Greece" => "Athens",
"Ireland"=>"Dublin", "Netherlands"=>"Amsterdam",
"Portugal"=>"Lisbon", "Spain"=>"Madrid", 
"Sweden"=>"Stockholm", "United Kingdom"=>"London", 
"Cyprus"=>"Nicosia", "Lithuania"=>"Vilnius", 
"Czech Republic"=>"Prague", "Estonia"=>"Tallin",
"Hungary"=>"Budapest", "Latvia"=>"Riga", "Malta"=>"Valetta", 
"Austria" => "Vienna", "Poland"=>"Warsaw"); 

$index = 0;
ksort($EU);
asort($index); 
While ($index <11)
    {
    echo " The Capitol of {$EU[$index]} is. <br/>";
    $index++;
    }


?>
  • If you look at the warnings that PHP is giving you, you will be on your way to fixing at least the sorting. I think you might need to look at the API docs for ksort, asort as well. Look here: http://sandbox.onlinephpfunctions.com/ –  Oct 12 '17 at 17:20
  • _"so i used ksort as well as asort to get them both to return"_ - both of those _sort_ the array, but they do not change anything about its structure. Are you actually looking for the functions array_keys and/or array_values maybe ...? – CBroe Oct 12 '17 at 17:20
  • `$index = 0; asort($index);` - this makes no sense whatsoever, you can not "sort" a single integer. – CBroe Oct 12 '17 at 17:22
  • and @CBroe, the crazy thing is my professor constantly said that you can use while loop for anything. Which is why i was wondering if i should just say forget it and run the foreach loop instead. I just needed guidance in the right direction. Thank you for the feedback – D Caldwell Oct 12 '17 at 17:25
  • Basically every while, for and foreach loop can be rewritten as one of the other two types, too, yes. But you still need to provide the data in the right format that each of those different loop types can "deal" with first, resp. transform it into one. The problem here is not mainly the while loop, but that inside it you are trying to access the array elements using a numeric index ... which here you simply do not have. – CBroe Oct 12 '17 at 17:29
  • To gain an understanding of what your prof was saying, read https://stackoverflow.com/questions/22801/the-difference-between-loops, https://stackoverflow.com/questions/2698935/differences-between-a-while-loop-and-a-for-loop-in-php, https://stackoverflow.com/questions/12847502/for-loop-vs-while-loop-vs-foreach-loop-php – CBroe Oct 12 '17 at 17:34

1 Answers1

1

Maybe cause the index for "Rome" is "Italy", not a number.

Try foreach:

foreach ($EU as $nation => $capitol) {
echo "The capitol of {$nation} is {$capitol}.\n";
}

And the line asort($index) makes no sense, you’re actually sorting a number.

NoImaginationGuy
  • 1,795
  • 14
  • 24
  • I vote for foreach() as well. Self terminates, clean, no fuss or dangerous rampant loops (that a while() has good chances of doing). Would: echo "The capitol of {$nation} is {$capitol}.\n"; would be better in your example? – IncredibleHat Oct 12 '17 at 17:32