iveon
iveon

Reputation: 1

How to get the value of an array?

I am testing the models in zend project, I have a question about how to get the value of an array, I found it can not be done using $array[index];

this is the find method I am testing:

   static function find($name, $order=null, $limit=null, $offset=null) {
        return self::_selectAndBind(
                get_class(),
                        self::getDefaultAdapter()
                        ->select()
                        ->from('user')
                        ->where('name = ?', array($name))
                        ->order($order)
                        ->limit($limit, $offset)
        );
    }

this is the test case for find():

public function testUser2CanFind() {
        $this->assertNotNull($this->_model->find('yes'));

        $this->assertEquals(1, count($this->_model->find('yes')));

        print_r($this->_model->find('yes'));
        //$this->assertEquals('admin',$this->_model->find('yes')[0]->login);
    }

I want to get the the value of login name, so we I print_r($this->_model->find('yes')); it gives:

......Array
(
    [0] => Application_Model_User2 Object
        (
            [_table:protected] => user
            [_primary:protected] => Array
                (
                    [0] => id
                )

            [_primary_ai:protected] => id
            [_data:protected] => Array
                (
                    [id] => 1
                    [created] => 2011-05-03 09:41:2
                    [login] => admin
                    [password_hash] => c8ebe700df11
                    [name] => yes
                    [surname] =>
                    [gender] =>
                    [street] =>
                    [postal_code] =>
                    [city] =>
                    [mobile] =>
                    [homephone] =>
                    [email] =>
                    [is_active] => 1
                )

            [_data_changed:protected] => Array
                (
                )

            [_readonly:protected] => Array
                (
                    [0] => id
                )

            [_db:protected] =>
        )

)

how could I get the value of [login] => admin? I tried to use $this->_model->find('yes')[0], but it gives error, can anyone help?

Upvotes: 0

Views: 349

Answers (1)

KingCrunch
KingCrunch

Reputation: 131881

$entity = current($this->_model->find('yes'));
echo $entity->login;

Update:

If there are more then one element in this list, use the usual iteration

foreach($this->_model->find('yes') as $entity) 
  echo $entity->login;

Upvotes: 1

Related Questions