0

I have result like below.

   stdClass Object
    (
      [Test_Result] => stdClass Object
        (
          [Test] => Array
            (
                [0] => stdClass Object
                    (

                        [name] => test1
                    )

                [1] => stdClass Object
                    (

                        [name] => test2
                    )

                [2] => stdClass Object
                    (

                        [name] => test3
                    )

                [3] => stdClass Object
                    (

                        [name] => test4
                    )

                [4] => stdClass Object
                    (

                        [name] => test5
                    )

            )

      )

 )

I am trying to retrieve name and add them as option values for select tag.

I am expecting like below

 <select id="names" name="names" class="required-entry select">
                <option value="" selected="selected">Please select...</option>
                <option value="test1">test1</option>
                <option value="test2">test2</option>
                <option value="test3">test3</option>
                <option value="test4">test4</option>
                <option value="test5">test5</option>
            </select>

How can i do it in PHP? Please anyone suggest me, How retrieve each values from the above result?

6
  • What is your expected output and what does it look like? Commented Aug 16, 2018 at 11:25
  • 2
    you can simply used (array) $object; to convert into Cast Array Commented Aug 16, 2018 at 11:27
  • 2
    Possible duplicate of Convert multidimensional objects to array Commented Aug 16, 2018 at 11:30
  • Please check my updated question Commented Aug 16, 2018 at 11:31
  • Can u post your std object, without print_r/var_dump ? Commented Aug 16, 2018 at 11:32

2 Answers 2

2

$object->Test_Result->Test is an array of objects that just have a name property. If you just want that name property, run a map on that array to convert that array to just the names.

$names = array_map(function($item) {
    return $item->name;
}, $object->Test_Result->Test);

Read more on array_map here.

Sign up to request clarification or add additional context in comments.

1 Comment

Try something yourself, if you get stuck, let me know.
0

If your object is $obj, you can do it like this:

echo '<select id="names" name="names" class="required-entry select">';
echo '<option value="" selected="selected">Please select...</option>';
foreach($obj->Test_Result->Test as $key => $value){
    echo '<option value="'.$value->name.'">'.$value->name.'</option>';
}
echo '</select>';

1 Comment

This works, but you may want to clean up the code. No reason to use PHP/echos on the surrounding HTML and no need for $key.

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.