php中怎么把对象从数组中取出来

<?php
include 'DBConfig.php';
include 'DBHelper.php';
include 'TestDemoModel.php';
$dbhelper = new DBHelper();//创建DBHelper
$conns = $dbhelper->functionGetConn($t_Host, $t_UserName, $t_pwd);//打开链接
$dbhelper -> functionGetDB($t_db);//搜索需要操作的数据库
$stringsql = "select testUserName,testPassWorld from test1";
$dbquery = $dbhelper->functionExebute($stringsql);//执行操作
$dbhelper->functionClose($conns);//关闭链接
$arr = array();
while($row=mysql_fetch_array($dbquery))
{
$tm = new testModel();
$tm->testUserName=$row["testUserName"];
$tm->testPassWorld=$row["testPassWorld"];
$arr[] = $tm;
}
for($i=0;$i<count($arr);$i++){
$tmr = (object)$arr[i];
echo $tmr->testUserName.'------------'.$tmr->testPassWorld.'<br/>';
}
foreach($arr as $key => $val)
{
$tme = (object)$val;
echo $tme->testUserName.'============'.$tme->testPassWorld.'<br/>';
}
?>
为什么第一个for 不是foreach,$tmr->testUserName.'------------'.$tmr->testPassWorld.'<br/>';取不出任何值?
这是最后的结果
------------
------------
------------
------------
3============4
3============4
3============4
3============4

第1个回答  2016-01-07

    数组操作,查询指定的值或者key,一般使用array_search和in_array;

    如果已经有key了,可以直接$array[$key];

    php对数组操作函数(这个里面很多都比较常用,最好掌握):

参考:http://www.w3school.com.cn/php/php_ref_array.asp

第2个回答  2013-08-26
我会说
$tmr = (object)$arr[i];
少了一个$符号么
$tmr = (object)$arr[$i];

php以为你的i是常量啊,而且你也没定他的值,取都没取出来

楼主不细心啊本回答被提问者采纳
相似回答