如何结合2个不同阵列结果的Foreach循环?

时间:2017-10-18 10:30:14

标签: php arrays foreach shuffle

$dogruA = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$yanlisA = array("3", "4", "7", "8", "10");

shuffle($dogruA);
shuffle($yanlisA);

    foreach ($dogruA as $dogru) :
        if ($dogru != '') {
            ?>
                <input type="checkbox" name="answerSik[]"
                       value="<?php echo $dogru; ?>"><?php echo $dogru; ?>
            <?php
        }
    endforeach;

    foreach ($yanlisA as $yanlis) :
        if ($yanlis!= '') { ?>
                <input type="checkbox" name="answerSik[]"
                       value="0"><?php echo $yanlis; ?>
            <?php
        }
    endforeach;

我如何结合这两个foreach?

我试过这些:

foreach( $dogruA as $dogru and $yanlisA as $yanlis)

foreach (array_combine($dogruA, $yanlisA) as $dogru => $yanlis) {

但我没有成功。 我如何使用方法?

2 个答案:

答案 0 :(得分:1)

您可以使用

foreach (array_merge($dogruA, $yanlisA) as $dogru => $yanlis) {

Array_combine具有与此处所需的功能不同的功能

如上所述,如果您只需要唯一值,则应使用此

foreach (array_unique(array_merge($dogruA, $yanlisA)) as $dogru => $yanlis) {

答案 1 :(得分:0)

您可以先合并数组,例如

$new_array = array_merge($dogruA, $yanlisA);

然后在使用array_unique()方法传递给foreach后,它会处理数组重复值,

foreach(array_unique($new_array) as $single){
 echo $single;
}