Deepanshu Jaiswal
Deepanshu Jaiswal

Reputation: 43

Unable to fetch the value from array

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

Upvotes: 1

Views: 75

Answers (3)

cypherabe
cypherabe

Reputation: 2603

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

Upvotes: 0

asd
asd

Reputation: 138

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

Upvotes: 0

Victory
Victory

Reputation: 5890

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
}

Upvotes: 2

Related Questions