jeudi 28 avril 2016

PHP in_array() not working properly

I have an if statment below that checks if a value is present in an array and adds it in there if it's not. Apparently even if the value is in the array, it triggers it again.

As far as I understand, it should only display 1 of each value, because it will only trigger 3 times, like this:

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

The code:

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>";

}

But it outputs:

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

Here is the 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" } 

Thank you for any help!

Aucun commentaire:

Enregistrer un commentaire