[英]How To Produce Multidimensional Array From While Loop?
I have this function : 我有这个功能:
function userKids(mysqli $Con, $Parent) {
$stmt = $Con->prepare('SELECT KidsName, KidsAge, KidsGender FROM Kids WHERE Parent = ?');
$stmt->bind_param('s', $Username);
$stmt->execute();
$Kid = null;
$Kids = array();
$stmt->bind_result($Kid, $KidsAge, $KidsGender);
while($stmt->fetch()) {
$Kids[] = $Kid;
}
return $Kids;
}
currently my WHILE loop produce array like this : 目前我的WHILE循环产生这样的数组:
Array ( [0] => john [1] => jane )
Now, how to produce multidimensional array so I can get the Age and Gender as well, like this : 现在,如何生成多维数组,以便我也可以获取年龄和性别,如下所示:
Array
(
[john] => Array
(
[0] => 3
[1] => male
)
[jane] => Array
(
[0] => 2
[1] => female
)
)
Change the line: 更改行:
$Kids[] = $Kid;
To: 至:
$Kids[$Kid] = array($KidsAge, $KidsGender);
I recommend that you use an associative array for the second dimension as well. 我建议您也对第二维使用关联数组。 So:
所以:
$Kids[$Kid] = array('age' => $KidsAge, 'gender' => $KidsGender);
replace the end of your code by: 将代码结尾替换为:
$stmt->bind_result($KidName, $KidAge, $KidGender);
while($stmt->fetch()) {
$Kids[$KidName] = array($KidAge,$KidGender);
}
return $Kids;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.