我有这段代码,
<?php foreach($information as $info) : ?>
<option selected="no" value="<?php echo $info['grade_id'];?>" id="<?php echo $info['grade_id'];?>">
<?php echo $info['grade_desc'];?>
</option>
<?php endforeach; ?>
基本上这样做是为选择菜单吐出一些选项,但有时选择选项会重复,有没有办法让我只吐出每个值的1个实例?使用我在for循环中已有的东西?有些人喜欢检查$info['grade_id']
是否与前一个匹配,是否跳过了那次迭代?
这就是信息数组的样子,
array(4) {
[0]=>
array(20) {
["career_id"]=>
string(2) "22"
["career_name"]=>
string(7) "Builder"
["career_desc"]=>
string(293) "Depending on the area of building that you would like to go into will depend on the aspects you will cover. Building covers- Civil Engineering, Electricians, Plunbers, Ground Workers, Brick Layers, Site Supervisors to name but a few. A career in construction can take you into many directions."
["degree_needed"]=>
string(2) "No"
["useful_info"]=>
NULL
["useful_links"]=>
string(45) "http://www.bbcl.co.uk,
http://www.abe.org.uk"
["salary_id"]=>
string(2) "20"
["basic_salary"]=>
NULL
["trained_salary"]=>
NULL
["progressed_salary"]=>
NULL
["average_salary"]=>
string(19) "Based on experience"
["careers_career_id"]=>
string(2) "22"
["grade_id"]=>
string(2) "53"
["grade_desc"]=>
string(103) "GCSE grade D in Maths, English and the successful completion of Diploma Level 1 Brickwork or equivalent"
["course_id"]=>
string(2) "52"
["course_type"]=>
string(24) "Classroom based learning"
["course_names"]=>
string(27) "Any combination of A Levels"
["extra_needed"]=>
NULL
["course_link"]=>
string(55) "http://www.blackburn.ac.uk/sixth_form_as_a2_levels.html"
["grades_grade_id"]=>
string(2) "53"
}
[1]=>
array(20) {
["career_id"]=>
string(2) "22"
["career_name"]=>
string(7) "Builder"
["career_desc"]=>
string(293) "Depending on the area of building that you would like to go into will depend on the aspects you will cover. Building covers- Civil Engineering, Electricians, Plunbers, Ground Workers, Brick Layers, Site Supervisors to name but a few. A career in construction can take you into many directions."
["degree_needed"]=>
string(2) "No"
["useful_info"]=>
NULL
["useful_links"]=>
string(45) "http://www.bbcl.co.uk,
http://www.abe.org.uk"
["salary_id"]=>
string(2) "20"
["basic_salary"]=>
NULL
["trained_salary"]=>
NULL
["progressed_salary"]=>
NULL
["average_salary"]=>
string(19) "Based on experience"
["careers_career_id"]=>
string(2) "22"
["grade_id"]=>
string(2) "53"
["grade_desc"]=>
string(103) "GCSE grade D in Maths, English and the successful completion of Diploma Level 1 Brickwork or equivalent"
["course_id"]=>
string(2) "53"
["course_type"]=>
string(24) "Practical based learning"
["course_names"]=>
string(19) "Bricklaying Level 2"
["extra_needed"]=>
string(3) "Yes"
["course_link"]=>
string(45) "http://www.blackburn.ac.uk/bricklaying_2.html"
["grades_grade_id"]=>
string(2) "53"
}
[2]=>
array(20) {
["career_id"]=>
string(2) "22"
["career_name"]=>
string(7) "Builder"
["career_desc"]=>
string(293) "Depending on the area of building that you would like to go into will depend on the aspects you will cover. Building covers- Civil Engineering, Electricians, Plunbers, Ground Workers, Brick Layers, Site Supervisors to name but a few. A career in construction can take you into many directions."
["degree_needed"]=>
string(2) "No"
["useful_info"]=>
NULL
["useful_links"]=>
string(45) "http://www.bbcl.co.uk,
http://www.abe.org.uk"
["salary_id"]=>
string(2) "20"
["basic_salary"]=>
NULL
["trained_salary"]=>
NULL
["progressed_salary"]=>
NULL
["average_salary"]=>
string(19) "Based on experience"
["careers_career_id"]=>
string(2) "22"
["grade_id"]=>
string(2) "54"
["grade_desc"]=>
string(96) "3 GCSE passes at grade D and above preferably in English, Maths, Science or a Technology subject"
["course_id"]=>
string(2) "54"
["course_type"]=>
string(16) "learn on the job"
["course_names"]=>
string(24) "Apprenticeship Brickwork"
["extra_needed"]=>
NULL
["course_link"]=>
string(56) "http://www.blackburn.ac.uk/apprentices_construction.html"
["grades_grade_id"]=>
string(2) "54"
}
[3]=>
array(20) {
["career_id"]=>
string(2) "22"
["career_name"]=>
string(7) "Builder"
["career_desc"]=>
string(293) "Depending on the area of building that you would like to go into will depend on the aspects you will cover. Building covers- Civil Engineering, Electricians, Plunbers, Ground Workers, Brick Layers, Site Supervisors to name but a few. A career in construction can take you into many directions."
["degree_needed"]=>
string(2) "No"
["useful_info"]=>
NULL
["useful_links"]=>
string(45) "http://www.bbcl.co.uk,
http://www.abe.org.uk"
["salary_id"]=>
string(2) "20"
["basic_salary"]=>
NULL
["trained_salary"]=>
NULL
["progressed_salary"]=>
NULL
["average_salary"]=>
string(19) "Based on experience"
["careers_career_id"]=>
string(2) "22"
["grade_id"]=>
string(2) "55"
["grade_desc"]=>
string(164) "Ascentis Entry Level 3 Certificate in Preparation for Employment in Construction Industries
A Functional Skills Certificate at Entry Level 3
A PSHE Certificate "
["course_id"]=>
string(2) "55"
["course_type"]=>
string(24) "Practical based learning"
["course_names"]=>
string(78) "Construction - Preparation for Employment in Construction Industries - Level 3"
["extra_needed"]=>
NULL
["course_link"]=>
string(74) "http://www.blackburn.ac.uk/preparation_for_employment_in_construction.html"
["grades_grade_id"]=>
string(2) "55"
}
}
你可以在输出中看到2个[grade_desc]相同的GCSE D级数学,英语和顺利完成文凭1级砖砌或同等学历。但是数据库中只有一条记录,我可以删除数组的任何重复数据吗?
答案 0 :(得分:2)
试
foreach(array_unique($information) as $info)
另外,请注意Ross的评论:首先优化数据库查询可能更好
加上,变量名“$ information”和“$ info”只是吮吸。我希望这只是一个例子!
答案 1 :(得分:1)
在 $ information 变量中使用 array_unique ,然后在foreach中使用它。
示例:强>
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
答案 2 :(得分:0)
使用存储最后成绩ID的其他变量:
$lastGradeID = null;
foreach ($information as $info) :
if ($lastGradeID !== $info['grade_id']) :
?>
…
<?php
endif;
$lastGradeID = $info['grade_id'];
endforeach;