英文:
How to filter multidimensional array based on column in a nested array?
问题
我想要过滤这个数组,所以它只包含值不为空的细节。Airco的“type”值为空,所以它不应返回“type”细节。在这种情况下,返回的数组应如下所示:
$array = [
0 => [
"label" => "Radiator",
"details" => 0 => [
"label" => "Condition",
"value" => "New",
],
1 => [
"label" => "Type",
"value" => "Wall",
],
],
1 => [
"label" => "Airco",
"details" => 0 => [
"label" => "Condition",
"value" => "New",
],
],
2 => [
"label" => "Refrigerator",
"details" => 0 => [
"label" => "Condition",
"value" => "Bad",
],
1 => [
"label" => "Type",
"value" => "Wall",
],
],
];
我知道我可以使用以下代码基于空列过滤数组(在这里找到):
$result = array_filter($array, function($o) use($column) {
return trim( $o[$column] ) !== '' && $o[$column] !== null;
});
但由于我有一个嵌套的数组“details”,我不太确定如何调整这段代码以使其适用于我的情况。
英文:
Let's say I have an array that looks like this:
$array = [
0 => [
"label" => "Radiator",
"details" => 0 => [
"label" => "Condition",
"value" => "New",
],
1 => [
"label" => "Type",
"value" => "Wall",
],
],
1 => [
"label" => "Airco",
"details" => 0 => [
"label" => "Condition",
"value" => "New",
],
1 => [
"label" => "Type",
"value" => "",
],
],
2 => [
"label" => "Refrigerator",
"details" => 0 => [
"label" => "Condition",
"value" => "Bad",
],
1 => [
"label" => "Type",
"value" => "Wall",
],
],
];
I want to filter this array, so it only includes details where the value is not empty. The value for type
of Airco is empty, so it should not return the detail type
. The returned array should in this case look like this:
$array = [
0 => [
"label" => "Radiator",
"details" => 0 => [
"label" => "Condition",
"value" => "New",
],
1 => [
"label" => "Type",
"value" => "Wall",
],
],
1 => [
"label" => "Airco",
"details" => 0 => [
"label" => "Condition",
"value" => "New",
],
],
2 => [
"label" => "Refrigerator",
"details" => 0 => [
"label" => "Condition",
"value" => "Bad",
],
1 => [
"label" => "Type",
"value" => "Wall",
],
],
];
I know I can filter an array based on an empty column with the following code (as found here):
$result = array_filter($array, function($o) use($column) {
return trim( $o[$column] ) !== '' && $o[$column] !== null;
});
but as I have a nested array details
I'm not quite sure how to adapt this code so it works for my case.
答案1
得分: 2
你的array_filter
只在第一层有效。你可能想要循环遍历details
数组,可以使用简单的foreach循环来实现。外层循环将遍历所有行,内层循环将遍历每行的details
。
foreach($array as &$row){
foreach($row['details'] as $key => $record){
if(strlen($record['value']) == 0){
unset($row['details'][$key]);
}
}
}
英文:
Your array_filter
is only working on the first level. You would rather want to loop on details
array too, which you can do using simple foreach loops. The outer loop will loop over all rows and the inner one over details
of each row.
<?php
foreach($array as &$row){
foreach($row['details'] as $key => $record){
if(strlen($record['value']) == 0){
unset($row['details'][$key]);
}
}
}
答案2
得分: 0
尝试这个,问题将会解决:
$array = [
[
"label" => "Radiator",
"details" => [
[
"label" => "Condition",
"value" => "New",
],
[
"label" => "Type",
"value" => "Wall",
],
],
],
[
"label" => "Airco",
"details" => [
[
"label" => "Condition",
"value" => "New",
],
[
"label" => "Type",
"value" => "",
],
],
],
[
"label" => "Refrigerator",
"details" => [
[
"label" => "Condition",
"value" => "Bad",
],
[
"label" => "Type",
"value" => "Wall",
],
],
],
];
// 这是魔法
echo "Old Array<pre>";
print_r($array); //die;
$newArray = [];
foreach ($array as $key => $value) {
$deatilsInside = [];
foreach ($value['details'] as $key1 => $value1) {
if ($value1['value'] != '') {
$deatilsInside[] = $value1;
}
}
$value['details'] = $deatilsInside;
$newArray[] = $value;
}
echo "New Array<pre>";
print_r($newArray);
die;
新的数组输出:
Array
(
[0] => Array
(
[label] => Radiator
[details] => Array
(
[0] => Array
(
[label] => Condition
[value] => New
)
[1] => Array
(
[label] => Type
[value] => Wall
)
)
)
[1] => Array
(
[label] => Airco
[details] => Array
(
[0] => Array
(
[label] => Condition
[value] => New
)
)
)
[2] => Array
(
[label] => Refrigerator
[details] => Array
(
[0] => Array
(
[label] => Condition
[value] => Bad
)
[1] => Array
(
[label] => Type
[value] => Wall
)
)
)
)
英文:
Try this and will resolved the problem
$array = [
[
"label" => "Radiator",
"details" => [[
"label" => "Condition",
"value" => "New",
],
[
"label" => "Type",
"value" => "Wall",
]],
],
[
"label" => "Airco",
"details" => [[
"label" => "Condition",
"value" => "New",
],
[
"label" => "Type",
"value" => "",
]],
],
[
"label" => "Refrigerator",
"details" => [[
"label" => "Condition",
"value" => "Bad",
],
[
"label" => "Type",
"value" => "Wall",
]],
],
];
Magic is here
echo "Old Array<pre>";
print_r($array);//die;
$newArray = [];
foreach($array as $key=>$value) {
$deatilsInside = [];
foreach($value['details'] as $key1=>$value1) {
if($value1['value'] != '') {
$deatilsInside[] = $value1;
}
}
$value['details'] = $deatilsInside;
$newArray[] = $value;
}
echo "New Array<pre>";
print_r($newArray);die;
New Array Output
Array
(
[0] => Array
(
[label] => Radiator
[details] => Array
(
[0] => Array
(
[label] => Condition
[value] => New
)
[1] => Array
(
[label] => Type
[value] => Wall
)
)
)
[1] => Array
(
[label] => Airco
[details] => Array
(
[0] => Array
(
[label] => Condition
[value] => New
)
)
)
[2] => Array
(
[label] => Refrigerator
[details] => Array
(
[0] => Array
(
[label] => Condition
[value] => Bad
)
[1] => Array
(
[label] => Type
[value] => Wall
)
)
)
)
答案3
得分: 0
你可以使用 array_map
和 array_filter
来完成这个任务:
$array = [
[
"label" => "散热器",
"details" => [
[
"label" => "状态",
"value" => "全新",
],
[
"label" => "类型",
"value" => "壁挂",
],
[
"label" => "",
"value" => "",
],
],
],
[
"label" => "空调",
"details" => [
[
"label" => "状态",
"value" => "全新",
],
[
"label" => "类型",
"value" => "",
],
],
],
[
"label" => "冰箱",
"details" => [
[
"label" => "状态",
"value" => "糟糕",
],
[
"label" => "类型",
"value" => "壁挂",
],
[
"label" => "",
"value" => "asd",
],
],
],
];
$filteredArray = array_map(function ($item) {
if (!empty($item["label"]) && !empty($item["details"])) {
$details = array_filter($item["details"], function ($detail) {
return !empty($detail["label"]) && !empty($detail["value"]);
});
$item["details"] = array_values($details);
return $item;
}
return null;
}, $array);
$filteredArray = array_filter($filteredArray);
print_r($filteredArray);
英文:
You can use array_map
and array_filter
to acomplish this:
$array = [
[
"label" => "Radiator",
"details" => [
[
"label" => "Condition",
"value" => "New",
],
[
"label" => "Type",
"value" => "Wall",
],
[
"label" => "",
"value" => "",
],
],
],
[
"label" => "Airco",
"details" => [
[
"label" => "Condition",
"value" => "New",
],
[
"label" => "Type",
"value" => "",
],
],
],
[
"label" => "Refrigerator",
"details" => [
[
"label" => "Condition",
"value" => "Bad",
],
[
"label" => "Type",
"value" => "Wall",
],
[
"label" => "",
"value" => "asd",
],
],
],
];
$filteredArray = array_map(function ($item) {
if (!empty($item["label"]) && !empty($item["details"])) {
$details = array_filter($item["details"], function ($detail) {
return !empty($detail["label"]) && !empty($detail["value"]);
});
$item["details"] = array_values($details);
return $item;
}
return null;
}, $array);
$filteredArray = array_filter($filteredArray);
print_r($filteredArray);
Above code will remove 3rd array from Radiator
, 2nd array from Airco
and 3rd array from Refrigerator
that have empty values for label
, value
or both.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论