1

我面临一个小问题,现在让我发疯。这可能是一件简单而愚蠢的事情,但好吧..我没有喝太多咖啡。

这是我打印时的数组:

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 个用户时,像这样:然后它不会在我的选择选项标签中显示名称,而只是显示“数组”。

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>";
        }
    }
}

但是,如果我删除一个用户,它就不会再显示任何东西了。在这种情况下,计数功能似乎无法按我想要的方式工作。请帮忙!

4

3 回答 3

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>"; 
    }
}
于 2013-08-22T08:05:11.353 回答
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 上的格式化程序对我不好:()

于 2013-08-22T08:11:37.540 回答
0
empty($arrayName)

尝试检查数组为空

于 2013-08-22T08:05:40.563 回答