如何根据嵌套数组中的列来筛选多维数组?

huangapple go评论74阅读模式
英文:

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]);
        }
    }
}

Live Demo

答案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 =    [    

        [  
            &quot;label&quot;    =&gt;  &quot;Radiator&quot;,  
            &quot;details&quot;  =&gt;   [[  
                                &quot;label&quot;     =&gt; &quot;Condition&quot;,  
                                &quot;value&quot;     =&gt; &quot;New&quot;,
                            ], 
                            [  
                                &quot;label&quot;     =&gt; &quot;Type&quot;,  
                                &quot;value&quot;     =&gt; &quot;Wall&quot;,
                            ]],
        ],  
        [  
            &quot;label&quot;    =&gt;  &quot;Airco&quot;,  
            &quot;details&quot;  =&gt;   [[  
                                &quot;label&quot;     =&gt; &quot;Condition&quot;,  
                                &quot;value&quot;     =&gt; &quot;New&quot;,
                            ], 
                            [  
                                &quot;label&quot;     =&gt; &quot;Type&quot;,  
                                &quot;value&quot;     =&gt; &quot;&quot;,
                            ]], 
        ], 
        [  
            &quot;label&quot;    =&gt;  &quot;Refrigerator&quot;,  
            &quot;details&quot;  =&gt;   [[  
                                &quot;label&quot;     =&gt; &quot;Condition&quot;,  
                                &quot;value&quot;     =&gt; &quot;Bad&quot;,
                            ], 
                            [  
                                &quot;label&quot;     =&gt; &quot;Type&quot;,  
                                &quot;value&quot;     =&gt; &quot;Wall&quot;,
                            ]], 
        ], 
    
    ];

Magic is here

    echo &quot;Old Array&lt;pre&gt;&quot;;
    print_r($array);//die;
    $newArray = [];
    foreach($array as $key=&gt;$value) {
        $deatilsInside = [];
        foreach($value[&#39;details&#39;] as $key1=&gt;$value1) {
            
            if($value1[&#39;value&#39;] != &#39;&#39;) {
                $deatilsInside[] = $value1;
            }
        }
        $value[&#39;details&#39;] = $deatilsInside;
        $newArray[] = $value;
    }

    echo &quot;New Array&lt;pre&gt;&quot;;
    print_r($newArray);die;

New Array Output

Array
(
    [0] =&gt; Array
        (
            [label] =&gt; Radiator
            [details] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [label] =&gt; Condition
                            [value] =&gt; New
                        )

                    [1] =&gt; Array
                        (
                            [label] =&gt; Type
                            [value] =&gt; Wall
                        )

                )

        )

    [1] =&gt; Array
        (
            [label] =&gt; Airco
            [details] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [label] =&gt; Condition
                            [value] =&gt; New
                        )

                )

        )

    [2] =&gt; Array
        (
            [label] =&gt; Refrigerator
            [details] =&gt; Array
                (
                    [0] =&gt; Array
                        (
                            [label] =&gt; Condition
                            [value] =&gt; Bad
                        )

                    [1] =&gt; Array
                        (
                            [label] =&gt; Type
                            [value] =&gt; Wall
                        )

                )

        )

)

答案3

得分: 0

你可以使用 array_maparray_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 = [
    [
        &quot;label&quot; =&gt; &quot;Radiator&quot;,
        &quot;details&quot; =&gt; [
            [
                &quot;label&quot; =&gt; &quot;Condition&quot;,
                &quot;value&quot; =&gt; &quot;New&quot;,
            ],
            [
                &quot;label&quot; =&gt; &quot;Type&quot;,
                &quot;value&quot; =&gt; &quot;Wall&quot;,
            ],
            [
                &quot;label&quot; =&gt; &quot;&quot;,
                &quot;value&quot; =&gt; &quot;&quot;,
            ],
        ],
    ],
    [
        &quot;label&quot; =&gt; &quot;Airco&quot;,
        &quot;details&quot; =&gt; [
            [
                &quot;label&quot; =&gt; &quot;Condition&quot;,
                &quot;value&quot; =&gt; &quot;New&quot;,
            ],
            [
                &quot;label&quot; =&gt; &quot;Type&quot;,
                &quot;value&quot; =&gt; &quot;&quot;,
            ],
        ],
    ],
    [
        &quot;label&quot; =&gt; &quot;Refrigerator&quot;,
        &quot;details&quot; =&gt; [
            [
                &quot;label&quot; =&gt; &quot;Condition&quot;,
                &quot;value&quot; =&gt; &quot;Bad&quot;,
            ],
            [
                &quot;label&quot; =&gt; &quot;Type&quot;,
                &quot;value&quot; =&gt; &quot;Wall&quot;,
            ],
            [
                &quot;label&quot; =&gt; &quot;&quot;,
                &quot;value&quot; =&gt; &quot;asd&quot;,
            ],
        ],
    ],
];

$filteredArray = array_map(function ($item) {
    if (!empty($item[&quot;label&quot;]) &amp;&amp; !empty($item[&quot;details&quot;])) {
        $details = array_filter($item[&quot;details&quot;], function ($detail) {
            return !empty($detail[&quot;label&quot;]) &amp;&amp; !empty($detail[&quot;value&quot;]);
        });

        $item[&quot;details&quot;] = 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.

huangapple
  • 本文由 发表于 2023年7月3日 16:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定