drxd54816 2014-04-18 15:09
浏览 50
已采纳

来自foreach循环的无法解释的输出

OK so I posted this question earlier and it remains unanswered. I am grateful for the attempts though.

Foreach loop not outputting arrays as expected

I have tried to narrow this down. I need to know if the array created from a string using explode is in some way different to an array specified by $foo = array(.....). Take this code

   $category_array = array('Pulmonary sarcoidosis', 'Acute critical care', 'Congenital lung disease');

    foreach($category_array as $category){
                $result = Thread_category::find_all_by_category($category);
                foreach ($result as $result_array){
                echo $result_array->thread_id;
                echo "<br/>";
                }
            }

    ?>

Yields:

145 146 149 151

144 148 150 151

145 147 148 149 151

Which is correct. But an array created as follows...

    $category_array = explode(",", $thread->category);

Which is when outputed gives

Array ( [0] => Pulmonary sarcoidosis [1] => Acute critical care [2] => Congenital lung disease )

But when going through the foreach loop gives...

145 146 149 151

It halts as after the first loop......

No idea why and been on this for hours outputting everything.....

I understand there is code in here I haven't explained but I think the problem has to be occuring at the explode level....please help!

  • 写回答

3条回答 默认 最新

  • doulan9188 2014-04-18 15:19
    关注

    Run this and tell us what the echo counts say, please.

    $category_array = explode(",","Pulmonary sarcoidosis,Acute critical care,Congenital lung disease");
    
    echo "category_array has ".count($category_array);
    
    foreach($category_array as $category){
                $result = Thread_category::find_all_by_category($category);
                echo "result variable has ".count($result); 
                foreach ($result as $result_array){
                echo $result_array->thread_id;
                echo "<br/>";
                }
            }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。