我有这个错误,我似乎无法解决。
这是代码
$this->stmt = $this->handle->prepare("SELECT * FROM tb_ph WHERE totalprofit = 0 AND status = '1' AND checks = ''");
$var = $this->stmt->fetchAll();
$var
返回一个多维数组,我喜欢循环,以获得各自的id和用户名。
现在的问题是,如果我做
foreach($var as $key => $value){
$id = $value['id'];
$username = $value['username'];
$phone = $value['phone'];
}
我收到通知:数组到字符串转换错误。
var_dump($var)
给出了
Array
(
[0] => Array
(
[id] => 3
[0] => 3
[username] => rose
[1] => rose
[phone] => +2782345578952
[2] => +2782345578952
)
[1] => Array
(
[id] => 4
[0] => 4
[username] => josh
[1] => rose
[phone] => +0182345578952
[2] => +0182345578952
)
)
每次返回两个结果时,如何使循环只获取id,用户名和电话?
答案 0 :(得分:1)
非常仔细地看这一点:
Array
(
[0] => Array
(
它向您显示您所拥有的是一个数组数组,因此您的foreach中的内容是另一个数组
foreach($var as $arr) {
/* here $arr is an array in calling print on it will produce the warning you have already seen but ...*/
print $arr[$id];
}
因为这里只打印一个数组元素。同样打印$ arr ['username']