2016-04-28 107 views
0

我在下面有一個if陳述,它檢查數組中是否存在值,如果不存在,則將其添加到數組中。 顯然,即使該值在數組中,也會再次觸發它。PHP in_array()無法正常工作

據我明白,它應該只每個值的顯示器1中,因爲它只會觸發3次,這樣的:

Digital Photography -> 0 
Step by Step Macintosh Training -> 0 
How to become a Powerful Speaker -> 0 

的代碼:

if (!in_array($unit['course_name'], $courseList)) { 

    $courseList[$unit['course_name']]['name'] = $unit['course_name']; 
    $courseList[$unit['course_name']]['seconds'] = 0; 
    echo $courseList[$unit['course_name']]['name'] . ' -> ' . $courseList[$unit['course_name']]['seconds']; 
    echo "<BR>"; 

} 

但它輸出:

Digital Photography -> 0 
Step by Step Macintosh Training -> 0 
Step by Step Macintosh Training -> 0 
Step by Step Macintosh Training -> 0 
How to become a Powerful Speaker -> 0 
How to become a Powerful Speaker -> 0 

這裏是var_dump($unit)

array(8) { ["author_name"]=> string(10) "tuiuiu_dev" [0]=> string(10) "tuiuiu_dev" ["course_name"]=> string(19) "Digital Photography" [1]=> string(19) "Digital Photography" ["unit_id"]=> string(3) "181" [2]=> string(3) "181" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" } 
array(8) { ["author_name"]=> string(15) "William Merussi" [0]=> string(15) "William Merussi" ["course_name"]=> string(31) "Step by Step Macintosh Training" [1]=> string(31) "Step by Step Macintosh Training" ["unit_id"]=> string(3) "227" [2]=> string(3) "227" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" } 
array(8) { ["author_name"]=> string(15) "William Merussi" [0]=> string(15) "William Merussi" ["course_name"]=> string(31) "Step by Step Macintosh Training" [1]=> string(31) "Step by Step Macintosh Training" ["unit_id"]=> string(3) "231" [2]=> string(3) "231" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" } 
array(8) { ["author_name"]=> string(15) "William Merussi" [0]=> string(15) "William Merussi" ["course_name"]=> string(31) "Step by Step Macintosh Training" [1]=> string(31) "Step by Step Macintosh Training" ["unit_id"]=> string(3) "233" [2]=> string(3) "233" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" } 
array(8) { ["author_name"]=> string(10) "tuiuiu_dev" [0]=> string(10) "tuiuiu_dev" ["course_name"]=> string(32) "How to become a Powerful Speaker" [1]=> string(32) "How to become a Powerful Speaker" ["unit_id"]=> string(4) "1080" [2]=> string(4) "1080" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" } 
array(8) { ["author_name"]=> string(10) "tuiuiu_dev" [0]=> string(10) "tuiuiu_dev" ["course_name"]=> string(32) "How to become a Powerful Speaker" [1]=> string(32) "How to become a Powerful Speaker" ["unit_id"]=> string(4) "1084" [2]=> string(4) "1084" ["unit_quantity"]=> string(1) "1" [3]=> string(1) "1" } 

謝謝你的幫助!

+1

在哪裏/如何最初分配'$ courseList'? –

+0

'$ courseList = array();'循環外部。 – William

回答

1

in_array()方法檢查數組值,而不是鍵。從你的例子我可以看到,作爲值你有一個數組與nameseconds字段。 我想你想檢查你的數組中是否存在該ID。

所以這if

if (!in_array($unit['course_name'], $courseList)) { 

應該是這樣的變化:

if (!isset($courseList[$unit['course_name']])) { 

這樣你檢查$unit['course_name']是你的陣列的關鍵英寸

+0

太完美了!它回答了我的問題,並教會我更多的PHP!隊友的歡呼聲! – William