【发布时间】:2017-03-28 22:09:13
【问题描述】:
使用 PHP 比较两个数组时出现以下错误。
错误:
注意:未定义的偏移量: /opt/lampp/htdocs/test/search.php 第 7 行中的 1
注意:未定义的偏移量: 1 在 /opt/lampp/htdocs/test/search.php 第 15 行
删除值注意:未定义的偏移量: /opt/lampp/htdocs/test/search.php 第 7 行中的 2
注意:未定义的偏移量: 2 in /opt/lampp/htdocs/test/search.php 第 15 行
删除值
我在下面解释我的代码。
$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
$childata=array(array('id'=>3),array('id'=>45));
for($i=0;$i<count($maindata);$i++){
//print_r($childata);
if(count($childata) > 0){
if(in_array($childata[$i],$maindata)){
echo "get the value \n".$maindata[$i]['id'];
echo "insert the value \n".$maindata[$i]['id'];
unset($childata[$i]);
if(count($childata) > 0){
$childata=array_values($childata);
}
}else{
echo "delete the value \n".$childata[$i]['id'];
unset($childata[$i]);
if(count($childata) > 0){
$childata=array_values($childata);
}
}
}else{
echo "get the value \n".$maindata[$i]['id'];
echo "insert the value \n".$maindata[$i]['id'];
}
}
请帮我解决这个错误。
【问题讨论】:
-
你想从条件中检查什么......
-
@PHPGeek :在这里我需要检查
$childata值是否存在于$maindata数组中,然后两个echo将执行并且该特定值将被删除。一旦$childata长度将是零,然后第二个 else 部分将执行。 -
那我觉得你需要一个一维数组而不是二维数组..这个数组有没有需要...
-
@PHPGeek : 你能把这个改正吗?
-
为什么不使用
array_udiff_assoc进行回调之类的?