如何修复array_values()期望参数1为数组,PHP中给出的字符串?

时间:2022-10-16 09:37:11

Details

  • I have 6 continents = {1,2,3,4,5,6}
  • 我有6个大陆= {1,2,3,4,5,6}
  • I want to loop through them and make a nice nav-menu out of them.
  • 我想要对它们进行循环,用它们做一个漂亮的导航菜单。

Here is what I have tried

   <nav id="nav">
    <ul class="container">

        <?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" ; 

            ?> 

            <!-- HTML go here  -->

            <li><a href="#<?php echo $continent ; ?>"><?php echo $continent ; ?> </a></li>


            <?php } ?>

        </ul>
    </nav>

Here is what I get

Error message say : array_values() expects parameter 1 to be array, string given !

错误消息说:array_values()希望参数1是数组,字符串给定!

Can someone tell me what I missed ?

有人能告诉我我错过了什么吗?

2 个解决方案

#1


1  

I fix it. Yay ! Thank you everyone for your comments. :D. You guys are awesome.

我修好它。耶!谢谢大家的评论。:D。你们都太棒了。

Here is my working code

<!-- Nav -->
<nav id="nav">
    <ul class="container">

        <?php 

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


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

            ?>

            <li><a href="#<?php echo $continent_name ?>"><?php echo $continent_name ?> </a></li>

            <?php }?>


        </ul>
    </nav>

Here is the result

如何修复array_values()期望参数1为数组,PHP中给出的字符串?

#2


0  

$continent = "Europe"

in first loop you change array into string - so IMHO you should change $continent to something else ;-)

在第一个循环中,你将数组转换成字符串-所以IMHO应该将$大陆转换为其他东西;-)

#1


1  

I fix it. Yay ! Thank you everyone for your comments. :D. You guys are awesome.

我修好它。耶!谢谢大家的评论。:D。你们都太棒了。

Here is my working code

<!-- Nav -->
<nav id="nav">
    <ul class="container">

        <?php 

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


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

            ?>

            <li><a href="#<?php echo $continent_name ?>"><?php echo $continent_name ?> </a></li>

            <?php }?>


        </ul>
    </nav>

Here is the result

如何修复array_values()期望参数1为数组,PHP中给出的字符串?

#2


0  

$continent = "Europe"

in first loop you change array into string - so IMHO you should change $continent to something else ;-)

在第一个循环中,你将数组转换成字符串-所以IMHO应该将$大陆转换为其他东西;-)