php CodeIgniter foreach循环返回值问题

5m1hhzi4  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(92)

我被CodeIgniter循环和数组卡住了,我想得到每个用户名和pid时间。

function UserStat()
    {       
        $this->db->select('username');      
        $this->db->where('status=','1');
        $query=$this->db->get("user_id");
        $uname=$query->result_array();
        
        foreach($uname as $name)
        {
            $this->db->select('SUM(pid) AS `UserTotalPid`');
            $this->db->like('user',$name['username']);
            $UsrTotalPidInSec['UserTotalPid']=$this->db->get('user_data')->row()->UserTotalPid;
            $data[]['username']=$name['username'];
            $data[]['UsrTotalPidInMin']=floor($UsrTotalPidInSec['UserTotalPid']/60).':'.gmdate('s',$UsrTotalPidInSec['UserTotalPid']);
        
        }
        return $data;
        //print_r($data);
    }
}

字符串

我现在得到这个回报这个

返回输出:[{"username":"Michle"},{"UsrTotalPidInMin":"1:58"},{"username":"Docker"},{"UsrTotalPidInMin":"0:55"},{"username":"Marchel"},{"UsrTotalPidInMin":"2:34"}]
print_r输出:Array ( [0] => Array ( [username] => Michle ) [1] => Array ( [UsrTotalPidInMin] => 1:58 ) [2] => Array ( [username] => Docker ) [3] => Array ( [UsrTotalPidInMin] => 0:55 ) [4] => Array ( [username] => Marchel ) [5] => Array ( [UsrTotalPidInMin] => 2:34 ) )

如果我把返回或打印数组内的循环,我得到这个

返回输出:[{"username":"Michle"},{"UsrTotalPidInMin":"1:58"}]
print_r输出:Array ( [0] => Array ( [username] => Michle ) [1] => Array ( [UsrTotalPidInMin] => 1:58 ) ) Array ( [0] => Array ( [username] => Michle ) [1] => Array ( [UsrTotalPidInMin] => 1:58 ) [2] => Array ( [username] => Docker ) [3] => Array ( [UsrTotalPidInMin] => 0:55 ) ) Array ( [0] => Array ( [username] => Michle ) [1] => Array ( [UsrTotalPidInMin] => 1:58 ) [2] => Array ( [username] => Docker ) [3] => Array ( [UsrTotalPidInMin] => 0:55 ) [4] => Array ( [username] => Marchel ) [5] => Array ( [UsrTotalPidInMin] => 2:34 ) )

但我的例外是这样

返回输出:[{"username":"Michle","UsrTotalPidInMin":"1:58"},{"username":"Docker","UsrTotalPidInMin":"0:55"},{"username":"Marchel","UsrTotalPidInMin":"2:34"}]
print_r输出:Array ( [username] => Michle [UsrTotalPidInMin] => 1:58 ) Array ( [username] => Docker [UsrTotalPidInMin] => 0:55 ) Array ( [username] => Marchel [UsrTotalPidInMin] => 2:34 )
我该如何解决这个问题?

brccelvz

brccelvz1#

感谢您提供您的实际和预期的结果。这帮助很大。
要解决阵列问题,您可以更改此部分...

$data[]['username']=$name['username'];
$data[]['UsrTotalPidInMin']=floor($UsrTotalPidInSec['UserTotalPid']/60).':'.gmdate('s',$UsrTotalPidInSec['UserTotalPid']);

字符串
像是...

$UsrTotalPidInMin = floor($UsrTotalPidInSec['UserTotalPid']/60).':'.gmdate('s',$UsrTotalPidInSec['UserTotalPid']);
$data[] = array('username'=>$name['username'],'UsrTotalPidInMin'=>$UsrTotalPidInMin);


在上面的代码中,我将$UsrTotalPidInMin作为一个单独的语句,以使$data[]语句更容易阅读。

相关问题