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.
Try this and will resolved the problem
New Array Output