Foreach循环在PHP中返回空值? [英] Foreach loop returning null values in PHP?

查看:278
本文介绍了Foreach循环在PHP中返回空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上我有一个名为 $ list 的数组,它是一个列表标题。如果我做一个 print_r($ list),我得到这些结果:

  Array([0] =>另一个新标题[1] => Awesome Movies and stuff [2] => Jascha's Title)

现在,我正在运行一个 foreach 循环来检索它们的值并将它们格式化为< ul> ;


$ b $ pre $函数get_film_list(){
global $ categories;
$ list = $ categories-> get_film_list();
if(count($ list)== 0){
echo'这个类别没有电影。
} else {
echo'< ul>';
foreach($ list为$ title){
echo'< li>'。 $ title。 <李>;
}
echo'< / ul>';




$ b我的问题是我的循环正在返回每个值有两个值(它是关键值?)
上述函数的结果如下所示:


  • 标题

  •  

  • 真棒电影和内容

  •  
  • Jascha的标题

  •  



b
$ b

  foreach($ list为$ key => $ title){
echo'< li>'。 $ title。 <李>;

$ / code $


结果相同:


  • 另一个新标题

  •  

  • 真棒电影




  • 我在这里错过了什么?



    在此先感谢。

    解决方案

    您使用< li> 代替< / li> 作为结束标记。使用正确的结束标记,它应该工作:

    $ p $ echo'< li>'。 $ title。 < /立GT;;


I have a pretty simple problem.

Basically I have an array called $list that is a list of titles. If I do a print_r($list) I get these results:

Array ( [0] => Another New Title [1] => Awesome Movies and stuff [2] => Jascha's Title )

Now, I'm running a foreach loop to retrieve their values and format them in an <ul> like so...

function get_film_list(){
    global $categories;
    $list = $categories->get_film_list();
    if(count($list)==0){
        echo 'No films are in this category';
    }else{
        echo '<ul>';
        foreach($list as $title){
           echo '<li>' . $title . '<li>';
        }
        echo '</ul>';
    }
}

The problem I'm having is my loop is returning two values per value (is it the key value?) The result of the preceding function looks like this:

  • Another New Title
  •  
  • Awesome Movies and stuff
  •  
  • Jascha's Title
  •  

I even tried:

foreach($list as $key => $title){
    echo '<li>' . $title . '<li>';
}

With the same results:

  • Another New Title
  •  
  • Awesome Movies and stuff
  •  
  • Jascha's Title
  •  

What am I missing here?

Thanks in advance.

解决方案

You’re using <li> instead of </li> as closing tag. Use the proper closing tag and it should work:

echo '<li>' . $title . '</li>';

这篇关于Foreach循环在PHP中返回空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆