php - associative arrays and array_merge -
print_r($element_attrs);
returns following arrays gathered xml file.
array ( [weather-summary] => thunderstorms ) array ( [weather-summary] => thunderstorms ) array ( [weather-summary] => chance thunderstorms ) array ( [weather-summary] => cloudy ) array ( [weather-summary] => sunny ) array ( [weather-summary] => clear ) array ( [weather-summary] => sunny ) array ( [weather-summary] => clear ) array ( [weather-summary] => sunny ) array ( [weather-summary] => partly cloudy ) array ( [weather-summary] => sunny ) array ( [weather-summary] => partly cloudy ) array ( [weather-summary] => sunny ) array ( [weather-summary] => rain fog/mist ) array ( )
right 16 separate associative arrays in keys same except last 1 that's blank reason. i'd merge these separate arrays 1 big array using array_merge()
or function can return second last value says "rain fog/mist"
right when print_r(array_merge($element_attrs));
get:
array ( [weather-summary] => thunderstorms ) array ( [weather-summary] => thunderstorms ) array ( [weather-summary] => chance thunderstorms ) array ( [weather-summary] => cloudy ) array ( [weather-summary] => sunny ) array ( [weather-summary] => clear ) array ( [weather-summary] => sunny ) array ( [weather-summary] => clear ) array ( [weather-summary] => sunny ) array ( [weather-summary] => partly cloudy ) array ( [weather-summary] => sunny ) array ( [weather-summary] => partly cloudy ) array ( [weather-summary] => sunny ) array ( [weather-summary] => rain fog/mist ) array ( )
assuming have array of of values of $element_attrs arrays should work.
function getlastweathersummary($arr){ $ret = array(); foreach($arr $summary){ if(array_key_exists("weather-summary", $summary)){ $ret[] = $summary["weather-summary"]; } } // if want whole array // return $ret // if care last 1 return array_pop($ret); }
if care last 1 when assigning each of $element_attrs values can
// loop through elements , set value of $element_attrs if (array_key_exists("weather-summary", $element_attrs)){ $last_summary = $element_attrs["weather-summary"]; }
after loop should end variable named $last_summary contains last defined value.
Comments
Post a Comment