如预期foreach循环不输阵 [英] Foreach loop not outputting arrays as expected
问题描述
我有一个名为对象 Thread_category
。这有一个名为 find_all_by_category($类)方法
,输出具有类线程的数组, $类别
。
我有一个数组 $ category_array_for_this_thread
,它输出:
阵列([0] =>变应性疾病[1] =>过敏性哮喘[2] =>先天性肺疾病[3] =>特发性肺纤维化)
现在,如果我这样做
$ test1的= Thread_category :: find_all_by_category('过敏性疾病');
的print_r($ test1的);
回声< BR />中;
回声< BR />中; $ TEST2 = Thread_category :: find_all_by_category('过敏性哮喘');
的print_r($测试2);
回声< BR />中;
回声< BR />中; $ TEST3 = Thread_category :: find_all_by_category('先天性肺病');
的print_r($ TEST3);
回声< BR />中;
回声< BR />中; $ TEST4 = Thread_category :: find_all_by_category(特发性肺纤维化');
的print_r($ TEST4);
回声< BR />中;
回声< BR />中;
我得到
阵列([0] => Thread_category对象([ID] => 2 [thread_id单] => 145类别] =>过敏性疾病)[1] = > Thread_category对象([ID] => 10的thread_id] => 146【类别】=>过敏性疾病)[2] => Thread_category对象([ID] => 20的thread_id] => 147【类别】=>过敏性疾病))阵列([0] => Thread_category对象([ID] => 21 [thread_id单] => 147【类别】=>哮喘)[1] => Thread_category对象([ID] => 24 [的thread_id] => 148【类别】=>过敏性哮喘))阵列([0] => Thread_category对象([ID] => 4的thread_id] => 145类别] =>先天性肺病)[1] => Thread_category对象([ID] => 22的thread_id] => 147【类别】=>先天性肺病)[2] => Thread_category对象([ID] => 25的thread_id] => 148【类别】=>先天性肺部疾病))阵列([0] => Thread_category对象([ID] => 5的thread_id] => 145类别] =>特发性肺纤维化)[1] => Thread_category对象([ID] => 11 [thread_id单] => 146【类别】=>特发性肺纤维化))
但是!
如果我这样做
的foreach($ category_array_for_this_thread为$类){
回声$类别。 < BR />中;
$ test_array = Thread_category :: find_all_by_category($类);
的print_r($ test_array);
}
我得到这个....
过敏性疾病
阵列([0] => Thread_category对象([ID] => 2 [thread_id单] => 145类别] =>过敏性疾病)[1] => Thread_category对象([ID] => 10 [thread_id单] => 146【类别】=>过敏性疾病)[2] => Thread_category对象([ID] => 20的thread_id] => 147【类别】=>过敏性疾病))
过敏性哮喘
阵列()
先天性肺部疾病
阵列()
特发性肺纤维化
阵列()
我能看到的唯一区别是,第二个是在foreach循环。为什么我收到3个空数组有人能向我解释
下面是方法......
公共静态功能find_all_by_category($类){
返回self ::的find_by_sql(SELECT * FROM thread_category WHERE类='{$}类');
}
您数组的第二个元素值为过敏
,但你需要有哮喘
。难道说,这将引发你的 find_all_by_category
功能 - 它解释了为什么第二个元素是空的,也许功能做一些奇怪的是没有发现赛后
I have an object called Thread_category
. This has a method called find_all_by_category($category)
which outputs an array of threads that have the category, $category
.
I have an array $category_array_for_this_thread
which outputs:
Array ( [0] => Allergic diseases [1] => Allergic asthma [2] => Congenital lung disease [3] => Idiopathic pulmonary fibrosis )
Now, if I do this
$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/>";
I get
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!
if I do this
foreach($category_array_for_this_thread as $category){
echo $category . "<br/>";
$test_array = Thread_category::find_all_by_category($category);
print_r($test_array);
}
I get this....
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 ( )
The only difference I can see is that the second is in a foreach loop. Can someone explain to me why I am getting 3 empty arrays
Here is the method....
public static function find_all_by_category($category) {
return self::find_by_sql("SELECT * FROM thread_category WHERE category='{$category}'");
}
The second element of your array has the value Allergic
but you need it to have Asthma
. Could it be that this throws your find_all_by_category
function off - it explains why the second element is empty, and maybe the function does something strange after it has found no match?
这篇关于如预期foreach循环不输阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!