使用一个数组过滤另一个数组的结果,两者都是多维的
Use one Array to filter result from another array, both are multidimensional
我从数据中提取一个数组,从 API 中提取一个数组。
API 数组 (array1)
...
[71] => Array
(
[id] => integer
[name] => example_name
[logo] => url_of_image
[lang] => en
)
...
数据库API(array2)
...
Array(
[0] => integer
[1] => integer
}
...
我想使用 array2 查找 array1 中 ID 的任何实例,如果它们存在则取消设置密钥。
数组差异不适用于多维。
没有通用密钥
这些值是动态的,所以我不能对它们进行硬编码
数组过滤器不会接受多个命令并且执行 foreach 会导致错误
Notice: Array to string conversion in
PHP5.6.
foreach($array2 as $key => $value) {
foreach($array1 as $key1 => $value1) {
if ($value1 == $value) {
unset($arrat2[$key])
}
}
}
我把它埋在某个地方以过滤掉我认为我可以在函数中插入一个 foreach 的语言,但这会引发数组到字符串的转换。
$filtered = array();
$filtered = array_filter($array1, function($el) { return ($el['lang'] == "en"); });
类似于:
$result = array();
foreach($array2 as $value) {
$result = array_filter($array1, function($ee) { return ($ee['id'] != $value); });
}
结果:
Notice: Undefined variable: value i
Var 转储数组 2:
array(2) {
[0]=>
array(1) {
["id"]=>
string(8) "random integer"
}
[1]=>
array(1) {
["id"]=>
string(9) "random integer"
}
}
var 转储数组 1:
array(151) {
[0]=>
array(4) {
["id"]=>
int(integer 1)
["name"]=>
string(7) "example name 1"
["logo"]=>
string(97) "image1.png"
["lang"]=>
string(2) "en"
}
[1]=>
array(4) {
["id"]=>
int(integer 2)
["name"]=>
string(10) "example name 2"
["logo"]=>
string(100) "image2.png"
["lang"]=>
string(2) "en"
}
[2]=>
array(4) {
["id"]=>
int(integer 3)
["name"]=>
string(9) "example name 3"
["logo"]=>
string(99) "image3.png"
["lang"]=>
string(2) "en"
}
很好的答案,还必须确保我的两个值都是被比较的整数。
我认为你的循环太多了。遍历要从中取消设置键的数组,并针对第二个数组测试每个值。
FOREACH ($array2){
$test_array[] = $id;
}
FOREACH ( $array1 AS $key => $array3){
IF ( in_array($array3['id'],$test_array) ) {
unset($array1[$key]);
}
}
这可能会有所帮助,具体取决于您要执行的操作(您可以在 $array2
中每行提供 1 个以上的过滤器,各行是“或”运算,而一行中的多个条件是“与”运算):
<?php
$array1 = array(
array(
"id" => 1,
"name" => "foo",
"logo" => "foo-logo.png",
"lang" => "es"
),
array(
"id" => 3,
"name" => "bar",
"logo" => "bar-logo.png",
"lang" => "en"
),
array(
"id" => 7,
"name" => "xyz",
"logo" => "xyz-logo.png",
"lang" => "it"
),
array(
"id" => 15,
"name" => "aaa",
"logo" => "aaa-logo.png",
"lang" => "it"
),
array(
"id" => 23,
"name" => "aaa",
"logo" => "aaa-logo.png",
"lang" => "es"
),
);
// Filter out entries with id #7 OR lang = en OR (name = "aaa" AND lang = "es")
$array2 = array(
array("id" => 7),
array("lang" => "en"),
array("name" => "aaa", "lang" => "es")
);
echo "<pre>";
$filtered = array_filter($array1, function($e) use ($array2)
{
foreach($array2 as $filters)
{
$doFilter = true;
foreach($filters as $k => $v)
{
if (isset($e[$k]) && $e[$k] !== $v) {
$doFilter = false;
break;
}
}
if ($doFilter)
{
return false;
}
}
return true;
});
var_dump($filtered);
输出:
array(2) {
[0]=>
array(4) {
["id"]=>
int(1)
["name"]=>
string(3) "foo"
["logo"]=>
string(12) "foo-logo.png"
["lang"]=>
string(2) "es"
}
[3]=>
array(4) {
["id"]=>
int(15)
["name"]=>
string(3) "aaa"
["logo"]=>
string(12) "aaa-logo.png"
["lang"]=>
string(2) "it"
}
}
我从数据中提取一个数组,从 API 中提取一个数组。
API 数组 (array1)
...
[71] => Array
(
[id] => integer
[name] => example_name
[logo] => url_of_image
[lang] => en
)
...
数据库API(array2)
...
Array(
[0] => integer
[1] => integer
}
...
我想使用 array2 查找 array1 中 ID 的任何实例,如果它们存在则取消设置密钥。
数组差异不适用于多维。 没有通用密钥 这些值是动态的,所以我不能对它们进行硬编码 数组过滤器不会接受多个命令并且执行 foreach 会导致错误
Notice: Array to string conversion in
PHP5.6.
foreach($array2 as $key => $value) {
foreach($array1 as $key1 => $value1) {
if ($value1 == $value) {
unset($arrat2[$key])
}
}
}
我把它埋在某个地方以过滤掉我认为我可以在函数中插入一个 foreach 的语言,但这会引发数组到字符串的转换。
$filtered = array();
$filtered = array_filter($array1, function($el) { return ($el['lang'] == "en"); });
类似于:
$result = array();
foreach($array2 as $value) {
$result = array_filter($array1, function($ee) { return ($ee['id'] != $value); });
}
结果:
Notice: Undefined variable: value i
Var 转储数组 2:
array(2) {
[0]=>
array(1) {
["id"]=>
string(8) "random integer"
}
[1]=>
array(1) {
["id"]=>
string(9) "random integer"
}
}
var 转储数组 1:
array(151) {
[0]=>
array(4) {
["id"]=>
int(integer 1)
["name"]=>
string(7) "example name 1"
["logo"]=>
string(97) "image1.png"
["lang"]=>
string(2) "en"
}
[1]=>
array(4) {
["id"]=>
int(integer 2)
["name"]=>
string(10) "example name 2"
["logo"]=>
string(100) "image2.png"
["lang"]=>
string(2) "en"
}
[2]=>
array(4) {
["id"]=>
int(integer 3)
["name"]=>
string(9) "example name 3"
["logo"]=>
string(99) "image3.png"
["lang"]=>
string(2) "en"
}
很好的答案,还必须确保我的两个值都是被比较的整数。
我认为你的循环太多了。遍历要从中取消设置键的数组,并针对第二个数组测试每个值。
FOREACH ($array2){
$test_array[] = $id;
}
FOREACH ( $array1 AS $key => $array3){
IF ( in_array($array3['id'],$test_array) ) {
unset($array1[$key]);
}
}
这可能会有所帮助,具体取决于您要执行的操作(您可以在 $array2
中每行提供 1 个以上的过滤器,各行是“或”运算,而一行中的多个条件是“与”运算):
<?php
$array1 = array(
array(
"id" => 1,
"name" => "foo",
"logo" => "foo-logo.png",
"lang" => "es"
),
array(
"id" => 3,
"name" => "bar",
"logo" => "bar-logo.png",
"lang" => "en"
),
array(
"id" => 7,
"name" => "xyz",
"logo" => "xyz-logo.png",
"lang" => "it"
),
array(
"id" => 15,
"name" => "aaa",
"logo" => "aaa-logo.png",
"lang" => "it"
),
array(
"id" => 23,
"name" => "aaa",
"logo" => "aaa-logo.png",
"lang" => "es"
),
);
// Filter out entries with id #7 OR lang = en OR (name = "aaa" AND lang = "es")
$array2 = array(
array("id" => 7),
array("lang" => "en"),
array("name" => "aaa", "lang" => "es")
);
echo "<pre>";
$filtered = array_filter($array1, function($e) use ($array2)
{
foreach($array2 as $filters)
{
$doFilter = true;
foreach($filters as $k => $v)
{
if (isset($e[$k]) && $e[$k] !== $v) {
$doFilter = false;
break;
}
}
if ($doFilter)
{
return false;
}
}
return true;
});
var_dump($filtered);
输出:
array(2) {
[0]=>
array(4) {
["id"]=>
int(1)
["name"]=>
string(3) "foo"
["logo"]=>
string(12) "foo-logo.png"
["lang"]=>
string(2) "es"
}
[3]=>
array(4) {
["id"]=>
int(15)
["name"]=>
string(3) "aaa"
["logo"]=>
string(12) "aaa-logo.png"
["lang"]=>
string(2) "it"
}
}