gpt4 book ai didi

php - 我想获得两个数组之间的差异

转载 作者:行者123 更新时间:2023-11-29 07:07:51 24 4
gpt4 key购买 nike

我有以下两个数组:$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

最佳答案

你可能不应该循环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

关于php - 我想获得两个数组之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40969941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com