我正面临一个小问题而且现在让我疯了。 它可能是简单而愚蠢的东西但很好......我没有多喝咖啡。
打印时,这是我的数组:
stdClass Object (
[SelectEmployeeResult] => stdClass Object (
[string] => Marijke Hakvoort ) )
我将字符串打印到选择菜单<select></select>
我是这样做的:
$employee = array ('pkrelation' => $_SESSION['username']);
$employeeResponse = $wcfclient->SelectEmployee($employee);
print_r($employeeResponse);
?>
<td><label>User:</label></td>
<td><select name="gebruiker">
<?php
if(count($employeeResponse) < 2){
foreach($employeeResponse->SelectEmployeeResult as $key => $value){
echo "<option>".$value."</option>";
}
}
但现在问题是:当我的对象数组中有2个用户时,如下所示:那么它不会在我的select选项标签中显示名称,而只是'array'。
stdClass Object (
[SelectEmployeeResult] => stdClass Object (
[string] => Array (
[0] => Marijke Hakvoort
[1] => User Test ) ) )
现在,我可以使用以下代码显示此用户:
if(count($employeeResponse) > 0){
foreach($employeeResponse->SelectEmployeeResult as $key => $value){
foreach($value as $key1 => $value1){
echo "<option>".$value1."</option>";
}
}
}
但是,如果我删除一个用户,它就不再显示任何内容了。 在这种情况下,count函数似乎不像我想要的那样工作.. 请帮忙!
答案 0 :(得分:3)
您必须稍微更改代码,因为您的代码是:
foreach($employeeResponse->SelectEmployeeResult as $key => $value){
echo "<option>".$value."</option>";
}
将其更改为:
foreach($employeeResponse->SelectEmployeeResult as $key => $value){
if(is_array($value)){
foreach($value as $k1 => $v1){
echo "<option>".$v1."</option>";
}
}else{
echo "<option>".$value."</option>";
}
}
答案 1 :(得分:2)
也许是这样的?
if (isset($employeeResponse->SelectEmployeeResult->string) && count($employeeResponse->SelectEmployeeResult->string) > 0) {
if (TRUE == is_array($employeeResponse->SelectEmployeeResult->string)) {
foreach ($employeeResponse->SelectEmployeeResult->string as $value) {
echo "<option>" . $value . "</option>";
}
} elseif (TRUE == is_string($employeeResponse->SelectEmployeeResult->string)) {
echo "<option>" . $employeeResponse->SelectEmployeeResult->string .
"</option>";
} }
(抱歉奇怪的布局... stackoverflow上的格式化程序对我不好:()
答案 2 :(得分:0)
empty($arrayName)
尝试使用数组空
进行检查