1

This is a array from which i want to extract the values [_answer:protected] and [_correct:protected]

Array
(
    [0] => Model_AnswerTypes Object
        (
            [_answer:protected] => True
            [_html:protected] => 
            [_points:protected] => 1
            [_correct:protected] => 1
            [_sortString:protected] => 
            [_sortStringHtml:protected] => 
            [_mapper:protected] => 
        )

    [1] => Model_AnswerTypes Object
        (
            [_answer:protected] => False
            [_html:protected] => 
            [_points:protected] => 1
            [_correct:protected] => 
            [_sortString:protected] => 
            [_sortStringHtml:protected] => 
            [_mapper:protected] => 
        )
)

What i am using

$key = '_answer:protected';
foreach ($array as $data)
    {
        echo $data[0]->$key;
    }

Getting a blank array out of this

Really appreciate any help

2
  • 4
    protected is a visibility of that variable. Commented Apr 3, 2014 at 7:54
  • Look at my answer to see possible resolutions to the protected visibility problem. Commented Apr 3, 2014 at 8:09

3 Answers 3

2

Assuming a bit about your model class i think the following may be what you want.

foreach ($array as $data)
{
    echo $data->answer; //(assumes Model_AnswerTypes::_get($name) is defined)
}

if that doesn't work, try

foreach ($array as $data)
{
    echo $data->getAnswer(); // assumes getter/setter pattern
}
Sign up to request clarification or add additional context in comments.

Comments

0
$key = '_answer:protected';
foreach ($array as $data)
{
    echo $data->$key; // The 0 is not needful because you make a foreach :)
} 

Comments

0

you have an array of objects, not straight values.

since the value of the Model_AnswerTypes object you want to read is protected, you need to use a method to get it (or the class need to use the __get() magic method).

usual methods are

$data->getAnswer();

or

$data->answer; //if the __get() method is implemented, a more unusual  form would be $data->_answer

1 Comment

this only gives the '_answer' key value how to extract '_correct' key value

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.