根据php中的条件取消设置数组元素

时间:2015-02-08 20:33:09

标签: php arrays nested

我应该如何根据多个条件循环嵌套数组和取消设置元素?从下面的数组开始,结果数组应仅包含period等于“2”且from时间不是当前日期的元素(即应该是明天或更晚)。后者可以通过简单地取消设置time子阵列中的前三个元素来实现,因为当period等于“2”并且from不在明天之前不可能在第四个元素之前发生。

这是原始数组:

array(2) {

    ["location"] => array(3){
        ["name"] => string(6) "Boston" 
        ["country"] => string(13) "United States" 
        ["timezone"] => array(1) { 
            ["@attributes"]=> array(2) { 
                ["id"]=> string(16) "America/New_York" 
                ["utcoffsetMinutes"]=> string(4) "-300" 
            } 
        } 
    } 

    ["forecast"] => array(1) { 
        ["tabular"] => array(1) { 
            ["time"]=> array(9) { 
                [0]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-08T16:00:00" 
                        ["to"]=> string(19) "2015-02-08T19:00:00" 
                        ["period"]=> string(1) "2" 
                    }  
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "4.5" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }  
                } 

                [1]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-08T19:00:00" 
                        ["to"]=> string(19) "2015-02-09T01:00:00" 
                        ["period"]=> string(1) "3" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.1" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 

                [2]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-09T01:00:00" 
                        ["to"]=> string(19) "2015-02-09T07:00:00" 
                        ["period"]=> string(1) "0" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.3" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 

                [3]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-09T07:00:00" 
                        ["to"]=> string(19) "2015-02-09T13:00:00" 
                        ["period"]=> string(1) "1" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.4" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 

                [4]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-09T13:00:00" 
                        ["to"]=> string(19) "2015-02-09T19:00:00" 
                        ["period"]=> string(1) "2" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.4" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 

                [5]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-09T19:00:00" 
                        ["to"]=> string(19) "2015-02-10T01:00:00" 
                        ["period"]=> string(1) "3" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.5" 
                            ["name"]=> string(13) "Moderate breeze" 
                        } 
                    }
                } 

                [6]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-10T01:00:00" 
                        ["to"]=> string(19) "2015-02-10T01:00:00" 
                        ["period"]=> string(1) "0" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "6.0" 
                            ["name"]=> string(13) "Moderate breeze" 
                        } 
                    }
                } 
                [7]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-10T07:00:00" 
                        ["to"]=> string(19) "2015-02-10T13:00:00" 
                        ["period"]=> string(1) "1" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.2" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 
                [8]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-10T13:00:00" 
                        ["to"]=> string(19) "2015-02-10T19:00:00" 
                        ["period"]=> string(1) "2" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "3.9" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 
            }
        }
    }
}

因此,当period等于“2”并且from明天后来生成的数组

array(2) {

    ["location"] => array(3){
        ["name"] => string(6) "Boston" 
        ["country"] => string(13) "United States" 
        ["timezone"] => array(1) { 
            ["@attributes"]=> array(2) { 
                ["id"]=> string(16) "America/New_York" 
                ["utcoffsetMinutes"]=> string(4) "-300" 
            } 
        } 
    } 

    ["forecast"] => array(1) { 
        ["tabular"] => array(1) { 
            ["time"]=> array(2) { 
                [0]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-09T13:00:00" 
                        ["to"]=> string(19) "2015-02-09T19:00:00" 
                        ["period"]=> string(1) "2" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "5.4" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 
                [1]=> array(2) { 
                    ["@attributes"]=> array(3) { 
                        ["from"]=> string(19) "2015-02-10T13:00:00" 
                        ["to"]=> string(19) "2015-02-10T19:00:00" 
                        ["period"]=> string(1) "2" 
                    }   
                    ["windSpeed"]=> array(1) { 
                        ["@attributes"]=> array(2) { 
                            ["mps"]=> string(3) "3.9" 
                            ["name"]=> string(13) "Gentle breeze" 
                        } 
                    }
                } 
            }
        }
    }
}

0 个答案:

没有答案