2

Details

  • I have 6 continent_id = { 1,2,3,4,5,6 }
  • I want it to loop 6 times and print out the appropriate continent name in stead.

Here what I have tried

<?php 

foreach(array_unique(array_values($continent)) as $continent_id){

    if($continent_id == 1 ){ $continent == "Europe" ; } 
    elseif ($continent_id == 2 ){ $continent == "Asia" ; } 
    elseif ($continent_id == 3 ){ $continent == "North America" ; } 
    elseif ($continent_id == 4 ){ $continent == "Oceania" ; } 
    elseif ($continent_id == 5 ){ $continent == "South America" ; } 
    else { $continent == "Africa" ; } 



    echo '<h1>#'.$continent."</h1>";

Here is what I get

I got this error message

Array to string conversion (View: C:\wamp\www\distributor-application\laravel\app\views\jsons\decoding.blade.php)

Can someone tell me what I missed ?

1
  • Here you use $continent as an array: array_values($continent) Here you compare it to a string: $continent == "Asia". Commented Jan 7, 2015 at 18:47

1 Answer 1

5

You're comparing with $continent == "Europe"

when you want to assign $continent = "Europe"

do that change and the same for the others.

Sign up to request clarification or add additional context in comments.

2 Comments

I would have mentioned that he should rename the second $continent variable to avoid future confusion with the code.
Most of the times it's the other way around :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.