[英]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.我在下面有一个
if
语句,用于检查数组中是否存在值,如果不存在则将其添加到那里。 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:据我了解,它应该只显示每个值的 1 个,因为它只会触发 3 次,如下所示:
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)
:这是
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!感谢您的帮助!
in_array()
method check array values, not keys. in_array()
方法检查数组值,而不是键。 From your example I can see that as value you have an array with name
and seconds
fields.从您的示例中,我可以看到您有一个包含
name
和seconds
字段的数组作为值。 I think you want to check if that id exists in your array.我认为您想检查该 id 是否存在于您的数组中。
So this if
:所以
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.这样您就可以检查
$unit['course_name']
是否在您的数组中作为键。
If you have an array with only values you should use in_array
function as this example:如果你有一个只有值的数组,你应该使用
in_array
函数作为这个例子:
$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:否则,如果你有一个带键的数组,你应该使用
isset
函数作为这个例子:
$array = array("A" => "Result A", "B" => "Result B", "C" => "Result C");
isset($array[$string]) ? "found" : "not found";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.