print_r($element_attrs);
返回从xml文件收集的以下数组。
Array ( [WEATHER-SUMMARY] => Thunderstorms )
Array ( [WEATHER-SUMMARY] => Thunderstorms Likely )
Array ( [WEATHER-SUMMARY] => Chance Thunderstorms )
Array ( [WEATHER-SUMMARY] => Mostly Cloudy )
Array ( [WEATHER-SUMMARY] => Mostly Sunny )
Array ( [WEATHER-SUMMARY] => Mostly Clear )
Array ( [WEATHER-SUMMARY] => Sunny )
Array ( [WEATHER-SUMMARY] => Mostly Clear )
Array ( [WEATHER-SUMMARY] => Sunny )
Array ( [WEATHER-SUMMARY] => Partly Cloudy )
Array ( [WEATHER-SUMMARY] => Mostly Sunny )
Array ( [WEATHER-SUMMARY] => Partly Cloudy )
Array ( [WEATHER-SUMMARY] => Mostly Sunny )
Array ( [WEATHER-SUMMARY] => Rain Fog/Mist )
Array ( )
现在它们是16个单独的关联数组,其中除了由于某种原因空白的最后一个键之外,所有键都是相同的。我想使用array_merge()
或其他函数将这些单独的数组合并为一个大数组,这样我就可以返回倒数第二个值“Rain Fog / Mist”
现在当我print_r(array_merge($element_attrs));
时,我得到了:
Array ( [WEATHER-SUMMARY] => Thunderstorms )
Array ( [WEATHER-SUMMARY] => Thunderstorms Likely )
Array ( [WEATHER-SUMMARY] => Chance Thunderstorms )
Array ( [WEATHER-SUMMARY] => Mostly Cloudy )
Array ( [WEATHER-SUMMARY] => Mostly Sunny )
Array ( [WEATHER-SUMMARY] => Mostly Clear )
Array ( [WEATHER-SUMMARY] => Sunny )
Array ( [WEATHER-SUMMARY] => Mostly Clear )
Array ( [WEATHER-SUMMARY] => Sunny )
Array ( [WEATHER-SUMMARY] => Partly Cloudy )
Array ( [WEATHER-SUMMARY] => Mostly Sunny )
Array ( [WEATHER-SUMMARY] => Partly Cloudy )
Array ( [WEATHER-SUMMARY] => Mostly Sunny )
Array ( [WEATHER-SUMMARY] => Rain Fog/Mist )
Array ( )
答案 0 :(得分:0)
假设你有一个$ element_attrs数组的所有值的数组,这应该可以。
function getLastWeatherSummary($arr){
$ret = array();
foreach($arr as $summary){
if(array_key_exists("WEATHER-SUMMARY", $summary)){
$ret[] = $summary["WEATHER-SUMMARY"];
}
}
// if you want the whole array
// return $ret
// if you only care about the last one
return array_pop($ret);
}
如果您只关心分配每个$ element_attrs值的最后一个,那么
// loop through elements and set the value of $element_attrs
if (array_key_exists("WEATHER-SUMMARY", $element_attrs)){
$last_summary = $element_attrs["WEATHER-SUMMARY"];
}
在循环之后,你应该得到一个名为$ last_summary的变量,它包含最后定义的值。
答案 1 :(得分:0)
$array = array(
array('WEATHER-SUMMARY' => 'Thunderstorms'),
array('WEATHER-SUMMARY' => 'Thunderstorms Likely'),
array('WEATHER-SUMMARY' => 'Chance Thunderstorms'),
array('WEATHER-SUMMARY' => 'Mostly Cloudy'),
array('WEATHER-SUMMARY' => 'Mostly Sunny'),
array()
);
array_filter()
和array_map()
:$flatArray = array_filter(array_map(function($a) {
if (!empty($a['WEATHER-SUMMARY'])) {
return $a['WEATHER-SUMMARY'];
}
}, $array));
foreach()
循环:$flatArray = array();
foreach ($array as $a) {
if (!empty($a['WEATHER-SUMMARY'])) {
$flatArray[] = $a['WEATHER-SUMMARY'];
}
}
然后,您可以使用array_pop()
获取最后一个数组条目:
echo array_pop($flatArray); // 'Mostly Sunny'
答案 2 :(得分:0)
首先,您需要收集$ element_attrs的所有不同值。因此,在进入循环之前,分配值会创建一个空数组。
$weatherSummaries = array();
在循环中循环遍历元素之后,在分配$ element_attrs变量之后,将与$ element_attrs的WEATHER-SUMMARY键相关联的值添加到weatherSummaries数组中(仅当它不为空时)
if (array_key_exists("WEATHER-SUMMARY", $element_attrs)){
$weatherSummaries[] = $element_attrs["WEATHER-SUMMARY"];
}
现在循环存在后,您应该有一个包含每个数组的WEATHER-SUMMARY元素的数组($ weatherSummaries)。
您可以通过引用来获取最后一个
$weatherSummaries[count($weatherSummaries) -1)];
答案 3 :(得分:-1)
它有点乱,但我最终使用css来制作所有这些,除了每组的最后一个看不见。