簡體   English   中英

如果值為空,則取消設置數組鍵

[英]Unset array key if value is empty

Array
(
    [2] => Array
        (
            [option_id] => 2
            [price] => 15
            [processor] => Array
                (
                    [3] => Array
                        (
                            [processor_id] => 3
                            [price] => 15
                        )

                    [4] => Array
                        (
                            [processor_id] => 4
                            [price] => 15
                        )

                )

        )

    [3] => Array
        (
            [option_id] => 3
            [price] => 15
            [processor] => Array
                (
                    [3] => Array
                        (
                            [processor_id] => 3
                            [price] => 15
                        )

                    [4] => Array
                        (
                            [processor_id] => 4
                            [price] => 15
                        )

                )

        )

    [4] => Array
        (
            [processor] => Array
                (
                    [3] => Array
                        (
                            [price] => // empty value
                        )

                    [4] => Array
                        (
                            [price] => // empty value
                        )

                )

        )

)

我有這個數組,現在我想取消設置該數組沒有任何值的數組,就像在最后一個數組中沒有給出任何值一樣,所以我想取消整個數組的鍵。

在此數組中,我的值為空,因此如何取消設置[4]鍵。 因此有可能沒有foreach循環。

您可以為此使用array_filter 由於您未提供任何代碼,因此建議您閱讀php.net上的文檔。

function filter_function($var) {
    // return an expression evaluating to true to keep value
    return ($var["option_id"]);
}

$filtered = array_filter($unfiltered, "filter_function");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM