我在下面有一个if
声明,用于检查数组中是否存在值,如果数值不存在则将其添加到数组中。
显然,即使值在数组中,它也会再次触发它。
据我了解,它应该只显示每个值中的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" }
感谢您的帮助!
答案 0 :(得分:1)
in_array()
方法检查数组值,而不是键。从您的示例中我可以看到,作为值,您有一个包含name
和seconds
字段的数组。
我想你想检查你的数组中是否存在该ID。
所以if
:
if (!in_array($unit['course_name'], $courseList)) {
应该这样改变:
if (!isset($courseList[$unit['course_name']])) {
通过这种方式,您可以检查数组中的$unit['course_name']
是否为密钥。
答案 1 :(得分:1)
如果您的数组仅包含值,则应使用in_array
函数作为此示例:
$array = array("A", "B", "C");
in_array($string, $array) ? "found" : "not found";
否则,如果您有一个包含键的数组,则应使用isset
函数作为此示例:
$array = array("A" => "Result A", "B" => "Result B", "C" => "Result C");
isset($array[$string]) ? "found" : "not found";