我在下面的foreach循环中添加了一个子数组。
从那时起,我收到以下错误,似乎引用了trim
语句的else
部分:
Warning: trim() expects parameter 1 to be string, array given in C:\inetpub\wwwroot\calendarCheck\index.php ...
有人可以告诉我如何调整它以使其适用于子数组? 来源: Add titles to items from array
我的代码:
$countries = '';
$valC = '';
$countC = 0;
foreach ($objDays->days as $days) {
if(($days->dateMatch == "Yes") && ($days->locales != "")) {
$inputC[] = array(
"text" => explode(',', $days->locales),
"desc" => $days->desc
);
$countC++;
}
}
if($countC == 0) {
$countries = " <img src='images/icons/emotion_what.png' alt='' /> No data on file for this date.";
} else {
$arrC = array_map("trim", call_user_func_array("array_merge", $inputC));
sort($arrC);
array_walk($arrC, function (&$valC) { $valC = "<img src='images/icons/flags-32/flag_" . str_replace(" ", "_", $valC['text']) . ".png' alt='".$valC['desc']."' id='c" . $valC['text'] . "' style='width:32px' /> " . $valC['text']; } );
$countries = implode(' ', $arrC);
}
// ...
echo $countries;
非常感谢你提供任何帮助,蒂姆。
答案 0 :(得分:2)
您也可以通过构建预期输出来执行此操作:(未经测试)
<?php
$countries = '';
foreach ($objDays->days as $days) {
if($days->dateMatch == "Yes" && !empty($days->locales)) {
foreach(explode(',', $days->locales) as $text){
$countries .= "
<img src='images/icons/flags-32/flag_".str_replace(" ", "_", $text).".png'
alt='".htmlentities($days->desc)."'
id='c".htmlentities($text)."'
style='width:32px' /> " . htmlentities($days->desc);
}
}
}
if(empty($countries)){
echo " <img src='images/icons/emotion_what.png' alt='' /> No data on file for this date.";
}else{
echo $countries;
}
?>
答案 1 :(得分:1)
您可以使用自定义功能代替trim
:
$arrC = array_map(function($x) {
$result = array();
foreach ($x as $k => $v)
$result[$k] = trim($v);
return $result;
}, call_user_func_array("array_merge", $inputC));