for 循环检查先前的 ID 并根据需要跳过迭代

发布于 2024-09-18 00:09:19 字数 6512 浏览 4 评论 0原文

我有这段代码,

<?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 以及成功完成 Diploma Level 1 Brickwork 或同等学历。但是数据库里只有一条记录,我可以去掉数组中的重复数据吗?

I have this code,

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

Basically what this does is spit of some options for a select menu, however sometimes the select options get duplicated, is there a way so I only ever spit out 1 instance of each value? Using what I already have in the for loop? Some like check the $info['grade_id'] does not match the previous one and if it does skip that iteration?

This is what the information array looks like,

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

You can see in the output there are 2 [grade_desc] the same GCSE grade D in Maths, English and the successful completion of Diploma Level 1 Brickwork or equivalent. However there is only one record in the database, can I just strip any repeated data of the array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-09-25 00:09:19

尝试

  foreach(array_unique($information) as $info)

加上,注意罗斯的评论:首先优化数据库查询可能会更好

,而且变量名“$information”和“$info”很糟糕。我希望这只是一个例子!

try

  foreach(array_unique($information) as $info)

plus, pay attention to Ross's comment: it might be better to optimize your db query in the first place

plus, variable names "$information" and "$info" just suck. I hope it's only an example!

爱本泡沫多脆弱 2024-09-25 00:09:19

$ 上使用 array_unique在 foreach 中使用信息变量之前。

示例:

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

检查上面的代码

use array_unique on $information variable before using it in foreach.

example:

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

Check above code

掩饰不了的爱 2024-09-25 00:09:19

使用一个附加变量来存储最后一个成绩 ID:

$lastGradeID = null;
foreach ($information as $info) :
    if ($lastGradeID !== $info['grade_id']) :
?>
…
<?php
    endif;
    $lastGradeID = $info['grade_id'];
endforeach;

Use an additional variable where you store the last grade ID:

$lastGradeID = null;
foreach ($information as $info) :
    if ($lastGradeID !== $info['grade_id']) :
?>
…
<?php
    endif;
    $lastGradeID = $info['grade_id'];
endforeach;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文