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
  • 1
    you can benefit a lot from Object Oriented Programming, Robert Commented Apr 19, 2013 at 7:05
  • 2
    What you want will overwrite results, if you have 2 kids with name "John". I hope you notice. Commented Apr 19, 2013 at 7:06
  • 1
    Try to put not KidsName as array key, but something unique, like IDKid, so it never might be repeated in SELECT result set. Or just leave your idea. Your choise. Commented Apr 19, 2013 at 7:17
  • @CORRUPT : you're such a brilliant man, bro! Commented Apr 19, 2013 at 7:18

3 Answers 3

4

Change the line:

$Kids[] = $Kid;

To:

$Kids[$Kid] = array($KidsAge, $KidsGender);
Sign up to request clarification or add additional context in comments.

1 Comment

As CORRUPT has mentioned, your solution will overwrite duplicate first names.
1

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

$Kids[$Kid] = array('age' => $KidsAge, 'gender' => $KidsGender);

Comments

1

replace the end of your code by:

$stmt->bind_result($KidName, $KidAge, $KidGender);
while($stmt->fetch()) {
    $Kids[$KidName] = array($KidAge,$KidGender);
}
return $Kids;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.