[英]inarray 2d array
I have a 2d array 我有一个2D阵列
$list['1'][] ='23';
$list['1'][] ='24';
$list['1'][] ='25';
$list['1'][] ='26';
And a 1d array 和1d阵列
$items={"23","24","36"};
following is right syntax for 1d array.
$items=array("23","24","36");
I want to check whether the $List['1'] values are inside the $items are not, if the items are not then store them into a new array 我想检查$ List ['1']值是否在$ items内部,如果这些项没有,则将它们存储到一个新数组中
I tried 我试过了
$different['1'][] = array_diff($items,$list['1']);
but if there are no difference it creates an array with key but empty value 但如果没有区别,它会创建一个带键但值为空的数组
$different['1] $不同['1]
['1'] => Array
(
[0] =>
)
if($diff = array_diff($items,$list['1])){
$different['1'][] = $diff;
}
This should do the trick 这应该可以解决问题
I cannot reproduce this behaviour. 我无法重现这种行为。
<?php
echo 'php: ', PHP_VERSION, "\n";
$list = array(1=>array());
$list[1][]=23;
$list[1][]=24;
$list[1][]=25;
$list[1][]=26;
$items = array(23,24);
var_dump(array_diff($items,$list[1]));
prints on my machine 在我的机器上打印
php: 5.4.1
array(0) {
}
try this: 尝试这个:
$item = array('23','24');
foreach($list['1'] as $listItem) {
if(!in_array($listItem, $item)) {
$different['1'][] = $listItem;
}
}
$list['1'][] ='23';
$list['1'][] ='24';
$items=array("23","24");
$different['1'][] = array_diff($items,$list['1']);
var_dump($different['1']); // output: array(1) { [0]=> array(0) { } }
$different['1'] = array_diff($items,$list['1']);
var_dump($different['1']); // output: array(0) { }
$list['1'][] ='23';
$list['1'][] ='24';
$list['1'][] ='25';
$list['1'][] ='26';
$items=array("23","24");
$different['1'][] = array_diff($list['1'],$items);
your arr_diff function's argument sequence was wrong. 你的arr_diff函数的参数序列是错误的。 thats why it will give you null. 这就是为什么它会给你null。 try above code. 尝试上面的代码。
Use for loop for every element in $list array and add following code in that loop 对$ list数组中的每个元素使用for循环,并在该循环中添加以下代码
if(!in_array($list[1][0], $items)){
array_push($items, $list[1][3]);
}
print_r($items);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.