如何获取另一个字符串数组中不存在的字符串数组的值?
I have an array $myArray1
with some string values like 2 5 7 13 23 25
. Another array $myArray2
has string values like 2 4 7 11 13 25
.
My requirement is to get a third array $myArray3
having only those values of $myArray1
that are not present in $myArray2
i.e. 5 23
.
I have tried $myArray3 = array_diff($myArray1, $myArray2)
but then $myArray3[0]
displays Undefined offset: 0.
Please tell me, is there any function of PHP for this?
我有一个数组 我的要求是获取第三个数组 我尝试了 请告诉我,PHP有什么功能吗? p>
div> $ myArray1 code>,其中包含一些字符串值,如
2 5 7 13 23 25 code>。 另一个数组
$ myArray2 code>包含字符串值,如
2 4 7 11 13 25 code>。 p>
$ myArray3 code>只有
$ myArray1 code>的值,
$ myArray2 code>,即
5 23 code>。 p>
$ myArray3 = array_diff($ myArray1,$ myArray2) code>,但
$ myArray3 [0] code>显示 Undefined offset:0 em>。 p>
array_diff
doesn't reset keys, so you get Undefined offset: 0
. If you want them to reset you need to use array_values
.
$myArray3 = array_values(array_diff($myArray1, $myArray2));
array_diff does not changes the index of the unique values. you can use array_values function to reset the indexes of result array.
try this
$result = array_values(array_diff($array , $array2));
print_r($result);