How should I go about to loop a nested array and unset elements based on multiple conditions? Beginning with the array below, the resulting array should contain only the elements where period
equals "2" and from
time is not the current day (ie should be tomorrow or later). The latter can be achieved by simply unsetting the first three elements in the time
subarray since when period
equals "2" and from
is not before tomorrow is not possible to happen before the fourth element.
This is the original array:
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"
}
}
}
}
}
}
}
So the resulting array when period
equals "2" and from
is tomorrow and later becomes
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"
}
}
}
}
}
}
}