我想得到两个数组之间的不同

时间:2016-12-05 08:25:17

标签: php html mysql codeigniter

我有两个数组:$array1$array。我想要这两个阵列之间的区别 也就是说,如何找到第二个数组中不存在的值?

$this->db->select('subject_id');
$this->db->where('section_id', $row['section_id']);
$this->db->where('day', $row['day']);

$progress = $this->db->get('class_routine')->result_array();
foreach ($progress as $key => $row3):

    $number1 = $row3['subject_id'];
    $array1 = explode(',', $number1);
    foreach ($array1 as $item1) {
        echo "<div style=\"color:#000;\">".$item1."</div>";
    }

endforeach;

$this->db->select('subject_id');
$this->db->where('section_id', $row['section_id']);
$this->db->where('day', $row['day']);
$progress = $this->db->get('progress')->result_array();
foreach ($progress as $row4):

    // echo $row4['subject_id'];

    $numbers = $row4['subject_id'];
    $array = explode(',', $numbers);
    foreach ($array as $items) {
        //echo $items;
        echo "<div style=\"color:red;\">".$items."</div>";

        $result = array_diff($array1, $array);

        echo "<div style=\"color:green;\">".$result."</div>";

    }

endforeach;

if($item1 != $items)
{
}  else {
    echo "All Present";
}

我尝试了以下PHP函数array_diff - 它回声为 以下内容:

1
2
3
2
Array
3
Array

2 个答案:

答案 0 :(得分:2)

你可能不应该循环array_diff。尝试更像这样的东西:

<?php
$array1 = array(....whatever goes in here....);
$array2 = array(....whatever goes in here....);
$result = array_diff($array1, $array2);  //will return an array

print_r($result);
?>

$array1中的多次出现都以相同的方式处理。这将输出一个包含差异的数组。

您可以从php.net

获取此类信息

来源:php.net/array_diff

答案 1 :(得分:0)

你可以在php.net中找到它

<?php
$array1    = array("a" => "green", "red", "blue", "red");
$array2    = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

它会显示这个

Array ( [1] => blue )