如何检查数组的元素是否存在于另一个数组数组中
How to check a element of array is present or not in another arrays of array
$result = array();
$A= Array
(
[0] => Array
(
[id] => 1946
[name] => cook
[pic] => cookpic.png
)
)
$B = Array
(
[0] => Array
(
[id] => 1944
[name] => driver
[pic] => driver.png
)
[1] => Array
(
[id] => 934
[name] => developer
[pic] => developer.jpg
)
[2] => Array
(
[id] => 1946
[name] => cook
[pic] => cookpic.png
)
)
我尝试过的:
$result = !empty(array_intersect($a, $b));
我有两个数组,我用var_dump
来显示它们并且可以在上面看到它们。
我想要实现的是,我想比较两个职业并且想要 $result
数组,这将是唯一的。现在,如果 Cook 不在 $B 中,那么 Cook 应该被推入 $B
数组,这将变为 $result
否则它不应该被推入。
做$result = !empty(array_intersect($a, $b));
只会告诉你$a
和$b
之间是否有共同元素
$result = array();
$a = array(
array(
"id" => 1946,
"name" => "Cook", // upper case C
"pic" => "cookpic.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
$b = array(
array(
"id" => 1944,
"name" => "driver",
"pic" => "driver.png"
),
array(
"id" => 934,
"name" => "developer",
"pic" => "developer.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
foreach ($a as $k => $v) {
if (in_array($v, $b) == false) {
array_push($result, $v);
}
}
print_r($result);
您可以使用 array_diff,但它不适用于多维数组。所以你必须从头开始
编辑:
$result = array();
$a = array(
array(
"id" => 1946,
"name" => "Cook", // upper case C
"pic" => "cookpic.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
$b = array(
array(
"id" => 1944,
"name" => "driver",
"pic" => "driver.png"
),
array(
"id" => 934,
"name" => "developer",
"pic" => "developer.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
$m = array_merge($a, $b);
$result = array_map("unserialize", array_unique(array_map("serialize", $m)));
print_r($result);
合并两个数组,然后应用 array_unique 使用 array_map 删除重复项
对 comparison.And 使用 strcasecmp()
内置函数,然后使用 array_push()
。
您可以使用蛮力方法。但我相信你可以从中得到更好的答案。
$result=array();
$A[0]=array("id" => 1946, "name" => Cook, "pic" => cookpic.png);
$B[0]=array("id" => 1944, "name" => driver, "pic" => driver.png);
$B[1]=array("id" => 934, "name" => developer, "pic" => developer.jpg);
$B[2]=array("id" => 1946, "name" => cook, "pic" => cookpic.png);
foreach ($A as $key => $value)
{
foreach ($B as $key1 => $value1)
{
if (!strcasecmp($value["name"], $value1["name"]))
{ array_push($result, $value); }
}
}
echo("<pre>");print_r($result);
$result = array();
$A= Array
(
[0] => Array
(
[id] => 1946
[name] => cook
[pic] => cookpic.png
)
)
$B = Array
(
[0] => Array
(
[id] => 1944
[name] => driver
[pic] => driver.png
)
[1] => Array
(
[id] => 934
[name] => developer
[pic] => developer.jpg
)
[2] => Array
(
[id] => 1946
[name] => cook
[pic] => cookpic.png
)
)
我尝试过的:
$result = !empty(array_intersect($a, $b));
我有两个数组,我用var_dump
来显示它们并且可以在上面看到它们。
我想要实现的是,我想比较两个职业并且想要 $result
数组,这将是唯一的。现在,如果 Cook 不在 $B 中,那么 Cook 应该被推入 $B
数组,这将变为 $result
否则它不应该被推入。
做$result = !empty(array_intersect($a, $b));
只会告诉你$a
和$b
$result = array();
$a = array(
array(
"id" => 1946,
"name" => "Cook", // upper case C
"pic" => "cookpic.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
$b = array(
array(
"id" => 1944,
"name" => "driver",
"pic" => "driver.png"
),
array(
"id" => 934,
"name" => "developer",
"pic" => "developer.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
foreach ($a as $k => $v) {
if (in_array($v, $b) == false) {
array_push($result, $v);
}
}
print_r($result);
您可以使用 array_diff,但它不适用于多维数组。所以你必须从头开始
编辑:
$result = array();
$a = array(
array(
"id" => 1946,
"name" => "Cook", // upper case C
"pic" => "cookpic.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
$b = array(
array(
"id" => 1944,
"name" => "driver",
"pic" => "driver.png"
),
array(
"id" => 934,
"name" => "developer",
"pic" => "developer.png"
),
array(
"id" => 1946,
"name" => "cook", // lower case c
"pic" => "cookpic.png"
)
);
$m = array_merge($a, $b);
$result = array_map("unserialize", array_unique(array_map("serialize", $m)));
print_r($result);
合并两个数组,然后应用 array_unique 使用 array_map 删除重复项
对 comparison.And 使用 strcasecmp()
内置函数,然后使用 array_push()
。
您可以使用蛮力方法。但我相信你可以从中得到更好的答案。
$result=array();
$A[0]=array("id" => 1946, "name" => Cook, "pic" => cookpic.png);
$B[0]=array("id" => 1944, "name" => driver, "pic" => driver.png);
$B[1]=array("id" => 934, "name" => developer, "pic" => developer.jpg);
$B[2]=array("id" => 1946, "name" => cook, "pic" => cookpic.png);
foreach ($A as $key => $value)
{
foreach ($B as $key1 => $value1)
{
if (!strcasecmp($value["name"], $value1["name"]))
{ array_push($result, $value); }
}
}
echo("<pre>");print_r($result);