简体   繁体   中英

Filter part of multidimensional array

I would like to filter part of a multidimensional array. I have used the array_filter function. When I print the filtered data, it shows correctly, but I can't seem to save the data back to the array.

Here is the multidimensional array (called $posted_product_details ) beforehand, containing the internal array ( [data] ) which I would like filter:

Array
(
[column_1] => Array
    (
        [name] => Colour
        [data] => Array
            (
                [0] => Blue
                [1] => Green
                [2] => Red
                [3] => Yellow
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
            )

    )

[column_2] => Array
    (
        [name] => Pack QTY
        [data] => Array
            (
                [0] => 3
                [1] => 3
                [2] => 3
                [3] => 3
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
            )

    )

[column_3] => Array
    (
        [name] => Product Code
        [data] => Array
            (
                [0] => 65030
                [1] => 65029
                [2] => 65028
                [3] => 65031
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
            )

    )

[column_4] => Array
    (
        [name] => Barcode
        [data] => Array
            (
                [0] => 5099570650307
                [1] => 5099570650291
                [2] => 5099570650284
                [3] => 5099570650314
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
            )

    )

[column_5] => Array
    (
        [name] => 
        [data] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
                [4] => 
                [5] => 
                [6] => 
                [7] => 
                [8] => 
                [9] => 
            )

    )

)

Here I attempt to loop through the array and filter the data:

foreach ($posted_product_details as $column => $info) {
    $name = $info['name'];
    $data = $info['data'];
    $info['data'] = array_filter($data);
}

However, upon printing the array afterward, the array has not changed.

Pass the value by reference to modify the original array:

foreach ($posted_product_details as $column => & $info) {
    $name = $info['name'];
    $data = $info['data'];
    $info['data'] = array_filter($data);
}

This will correctly filter the data part of your array. However, if you need to filter out deeper elements, you'll have to use a recursive function, such as this one .

Demo!

The foreach construct makes a copy of each piece of the array as you iterate over it. You have to explicitly call the original array to edit it:

$posted_product_details[$column]['data'] = array_filter($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM