0

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!

2
  • 1
    where/how is $courseList initially assigned? Commented Apr 28, 2016 at 15:58
  • $courseList = array(); outside the loop. Commented Apr 28, 2016 at 16:54

2 Answers 2

2

in_array() method check array values, not keys. From your example I can see that as value you have an array with name and seconds fields. I think you want to check if that id exists in your array.

So this if:

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

Should be change like this:

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

This way you check if the $unit['course_name'] is in your array as key.

Sign up to request clarification or add additional context in comments.

1 Comment

That was perfect! It answered my question and taught me some more PHP! Cheers mate!
1

If you have an array with only values you should use in_array function as this example:

$array = array("A", "B", "C");
in_array($string, $array) ? "found" : "not found";

Else if you have an array with keys you should use isset function as this example:

$array = array("A" => "Result A", "B" => "Result B", "C" => "Result C");
isset($array[$string]) ? "found" : "not found";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.