PHP + mysqli:将数组推入数组

I basically need to create an array like this: http://pastebin.com/BAfRnTLz

I have two different tables in my db. One for projects and one for tasks. Each task has a column for the project id it belongs to. I need to create a multidimensional array where I look at each tasks ID in relation to the projects id, and append that task to the array of the correct project.

I fear i i'm going in the wrong direction. I have been trying to make this work for two days now (very new at this). Any help is very much appreciated! Thanks in advance!

I have this so far

$projects = $mysqli->query("SELECT projectID, projectName FROM projects WHERE userID = '".$userID."'");
while (($row = $projects->fetch_assoc()) !== null) {
print_r($row);

$projectID = $row['projectID'];
$tasks = $mysqli->query("SELECT * FROM tasks WHERE projectID = '".$projectID."'");
while (($row = $tasks->fetch_assoc()) !== null) {
    print_r($row);
}

A join would be better suited for this but you could bolt them together like this assuming you don't have a field called tasks in projects.

$p = array();

$projects = $mysqli->query("SELECT projectID, projectName FROM projects WHERE userID = '".$userID."'");
while (($row = $projects->fetch_assoc()) !== null) {
    $p[$row['projectID']] = $row;
}

$projectID = $row['projectID'];
$tasks = $mysqli->query("SELECT * FROM tasks WHERE projectID = '".$projectID."'");
while (($row = $tasks->fetch_assoc()) !== null) {
    $p[$row['projectID']]['tasks'][] = $row;
}

print_r($p);