如何获取数组的值?
我正在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
如果此列表中有多个元素,请使用通常的迭代
Update:
If there are more then one element in this list, use the usual iteration