【发布时间】:2018-07-02 19:55:09
【问题描述】:
我遇到以下问题: 我有这个函数,我将查询结果($matches_lines)作为参数传递。在我的函数中,我从查询结果中获取了我需要的所有数据,并且我试图将它存储在一个多维数组中。我的代码如下:
function check_matches($matches_lines, $minage, $maxage, $actual_persontype){
$result = array(array());
$count = 0;
foreach($matches_lines as $lines){
$match_user = $lines["signup_username"];
$match_birth = $lines["signup_birth"];
$match_city = $lines["signup_city"];
$match_gender = $lines["signup_gender"];
$match_os = $lines["signup_os"];
$match_persontype = $lines["signup_persontype"];
if("some condition I want to verify"){
$new_add = array($match_user, $match_birth, $match_city, $match_gender, $match_os, $match_persontype);
array_push($result[$count], $new_add);
$count = $count+1;
}
}
return $result;
}
我只是简单地调用我的函数:
$matches_found = check_matches($matches_lines, $minage, $maxage, $actual_persontype);
这样做不会出错,但是当我尝试回显一行时
echo $matches_found[0][0];
我收到“HP 通知:未定义偏移量:0”。
我做错了什么?
编辑:var_dump($matches_found) 返回 "array(0) { }"
当 var_dump($matches_lines) 返回时
object(PDOStatement)#3 (1) { ["queryString"]=> string(277) "SELECT s.signup_username, s.signup_birth, s.signup_city, s.signup_gender, s.signup_os, s.signup_persontype FROM 注册 WHERE s.signup_username 'leonardo' && s.signup_city = 'Torino' && s.signup_os = 'Windows'" }
【问题讨论】:
-
另外,如果您不能给我们条件,请再次检查条件是否真的至少匹配一行。如果你能提供,请提供。
-
array_push通过引用获取其第一个参数(一个数组)。$result[$count]在$count = 0之后是未定义的(null),所以它不起作用。您不能将项目推送到 null 上。
标签: php mysql sql arrays multidimensional-array