0

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 :

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
  )
)
4

3 回答 3

4

Change the line:

$Kids[] = $Kid;

To:

$Kids[$Kid] = array($KidsAge, $KidsGender);
于 2013-04-19T07:04:43.093 回答
1

I recommend that you use an associative array for the second dimension as well. So:

$Kids[$Kid] = array('age' => $KidsAge, 'gender' => $KidsGender);
于 2013-04-19T07:06:26.383 回答
1

replace the end of your code by:

$stmt->bind_result($KidName, $KidAge, $KidGender);
while($stmt->fetch()) {
    $Kids[$KidName] = array($KidAge,$KidGender);
}
return $Kids;
于 2013-04-19T07:12:21.297 回答