Foreach循环不按预期输出数组

时间:2014-04-18 12:27:03

标签: php arrays foreach

我有一个名为Thread_category的对象。这有一个名为find_all_by_category($category)的方法,它输出一个具有类别$category的线程数组。

我有一个数组$category_array_for_this_thread输出:

Array ( [0] => Allergic diseases [1] => Allergic asthma [2] => Congenital lung disease [3] => Idiopathic pulmonary fibrosis ) 

现在,如果我这样做

    $test1 = Thread_category::find_all_by_category('Allergic diseases');
    print_r($test1);
    echo "<br/>";
    echo "<br/>";

    $test2 = Thread_category::find_all_by_category('Allergic asthma');
    print_r($test2);
    echo "<br/>";
    echo "<br/>";

    $test3 = Thread_category::find_all_by_category('Congenital lung disease');
    print_r($test3);
    echo "<br/>";
    echo "<br/>";

    $test4 = Thread_category::find_all_by_category('Idiopathic pulmonary fibrosis');
    print_r($test4);
    echo "<br/>";
    echo "<br/>";

我得到了

Array ( [0] => Thread_category Object ( [id] => 2 [thread_id] => 145 [category] => Allergic diseases ) [1] => Thread_category Object ( [id] => 10 [thread_id] => 146 [category] => Allergic diseases ) [2] => Thread_category Object ( [id] => 20 [thread_id] => 147 [category] => Allergic diseases ) ) 

Array ( [0] => Thread_category Object ( [id] => 21 [thread_id] => 147 [category] => Asthma ) [1] => Thread_category Object ( [id] => 24 [thread_id] => 148 [category] => Allergic asthma ) ) 

Array ( [0] => Thread_category Object ( [id] => 4 [thread_id] => 145 [category] => Congenital lung disease ) [1] => Thread_category Object ( [id] => 22 [thread_id] => 147 [category] => Congenital lung disease ) [2] => Thread_category Object ( [id] => 25 [thread_id] => 148 [category] => Congenital lung disease ) ) 

Array ( [0] => Thread_category Object ( [id] => 5 [thread_id] => 145 [category] => Idiopathic pulmonary fibrosis ) [1] => Thread_category Object ( [id] => 11 [thread_id] => 146 [category] => Idiopathic pulmonary fibrosis ) )

BUT!

如果我这样做

        foreach($category_array_for_this_thread as $category){
                    echo $category . "<br/>";
        $test_array = Thread_category::find_all_by_category($category);
        print_r($test_array);
        }

我明白了......

   Allergic diseases
   Array ( [0] => Thread_category Object ( [id] => 2 [thread_id] => 145 [category] =>    Allergic diseases ) [1] => Thread_category Object ( [id] => 10 [thread_id] => 146 [category] => Allergic diseases ) [2] => Thread_category Object ( [id] => 20 [thread_id] => 147 [category] => Allergic diseases ) ) 
   Allergic asthma
   Array ( ) 
   Congenital lung disease
   Array ( ) 
   Idiopathic pulmonary fibrosis
   Array ( )

我能看到的唯一区别是第二个是foreach循环。有人可以向我解释为什么我得到3个空阵列

这是方法......

       public static function find_all_by_category($category) {
    return self::find_by_sql("SELECT * FROM thread_category WHERE category='{$category}'");

}

2 个答案:

答案 0 :(得分:0)

在foreach循环中,你的$ category只有一个值,而其他3个可能是空的,或者有任何其他东西在搜索时没有返回任何结果。

答案 1 :(得分:0)

数组的第二个元素的值为Allergic,但您需要Asthma。可能是这会抛出你的find_all_by_category函数 - 它解释了为什么第二个元素是空的,也许这个函数在找不到匹配后做了一些奇怪的事情?