如何获取数组的值?

发布于 2024-11-07 05:50:54 字数 2325 浏览 0 评论 0原文

我正在zend项目中测试模型,我有一个关于如何获取数组值的问题,我发现它不能使用$array[index]来完成;

这是我正在测试的 find 方法:

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

这是 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);
    }

我想获取登录名的值,所以我们 print_r($this->_model->find(' yes')); 它给出:

......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] =>
        )

)

我如何获得 [login] =>; 的值管理员?我尝试使用 $this->_model->find('yes')[0],但出现错误,有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-11-14 05:50:54
$entity = current($this->_model->find('yes'));
echo $entity->login;

更新:

如果此列表中有多个元素,请使用通常的迭代

foreach($this->_model->find('yes') as $entity) 
  echo $entity->login;
$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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文