1

I'm facing a little problem and it's driving me nuts right now. It's probably something easy and stupid but well.. I haven't had much coffee.

This is my array when I print it:

stdClass Object ( 
[SelectEmployeeResult] => stdClass Object ( 
[string] => Marijke Hakvoort ) )

I print the string into a select menu <select></select>

I do this in this way:

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

But now the problem: When I have 2 users in my object array, like this: then it doesn't show the names in my select option tags, but just 'array'.

stdClass Object ( 
[SelectEmployeeResult] => stdClass Object ( 
[string] => Array ( 
[0] => Marijke Hakvoort 
[1] => User Test ) ) )

Now, I can show this users by using this code:

if(count($employeeResponse) > 0){
    foreach($employeeResponse->SelectEmployeeResult as $key => $value){
        foreach($value as $key1 => $value1){
            echo "<option>".$value1."</option>";
        }
    }
}

But if I delete one user, it doesn't show me anything anymore. The count function doesn't seem to work the way I want in this case.. Please help!

3 Answers 3

3

You have to change code little bit as your code is:

foreach($employeeResponse->SelectEmployeeResult as $key => $value){
    echo "<option>".$value."</option>"; 
}

Change it to:

foreach($employeeResponse->SelectEmployeeResult as $key => $value){
    if(is_array($value)){
       foreach($value as $k1 => $v1){
        echo "<option>".$v1."</option>";                
       }
    }else{
        echo "<option>".$value."</option>"; 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thanks! Didn't even think that way! You saved my day sir.
2

Perhaps something like this?

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

(sorry for the weird layout... formatter on stackoverflow is not nice to me :( )

2 Comments

Vineet1982 already gave me an excellent answer, but yours is fine too :) Thanks!
I added the correct count because you stated you also had a problem with that ;)
0
empty($arrayName)

try to check with array empty

4 Comments

Why would this work? The array is never empty cause it will always have 1 or more values in it.
@Ashish, foreach will only iterate if array/object have 1 or more value.
@Marijke array may be empty run the following code $array = array(); var_dump(empty($array));
The array is never empty, thats not possible because my program always adds 1 admin user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.