我正在分析在调试运行时类变量的类型和值时发现的其他代码:
echo print_r($this->_out);
Array
(
[id] => -1
[fieldErrors] => Array
(
)
[error] =>
[data] => Array
(
)
[row] => Array
(
[DT_RowId] => row_177
[id] => 177
[last_name] => sdfdsf
[first_name] => dsf
[homeaddr] => sdfdsfsdfdsfdsfdsfdsf
[email] => s@jj.com
[officeaddr] => wwwwwwwwwwwwwwwwwwwwwwww
[mobile] => 11111111
[age] => 11
[chargeamt] => 11
[start_date] => 11/11/2011
)
)
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sdfdsf","first_name":"dsf","homeaddr":"sdfdsfsdfdsfdsfdsfdsf","email":"s@jj.com","officeaddr":"wwwwwwwwwwwwwwwwwwwwwwww","mobile":"11111111","age":"11","chargeamt":"11","start_date":"11\/11\/2011"}}
我是PHP新手,想知道如何访问 [id] => 177 值,即值177。
我尝试了很多方法
$this->_out['row']['id'][0]
它给了我以下结果:
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss@ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
,而
我尝试了很多方法
$this->_out['row']['id']
它给了我以下结果:
177{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss@ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
和其他人,但它只是没有给我预期的。
如何根据需要访问该值?
答案 0 :(得分:0)
print_r结果是一个包含更多数组的数组。首先,您必须找到主数组的索引,该索引表示具有您要查找的值的子数组。然后,您必须使用此索引来访问子数组值。
答案 1 :(得分:0)
你做得对。 $this->_out['row']['id']
将返回所需的结果(请检查为什么还会获得不属于print_t($this->_out)
的JSON字符串。
这将返回结果177
:
$this->_out['row']['id'];
因为在PHP中你可以访问字符串字符作为数组,这将返回字符串中的第一个字符(即1
):
$this->_out['row']['id'][0];
这会抛出错误,因为没有这样的索引(字符串长度是3,所以最后一个索引是2):
$this->_out['row']['id'][5];