B. Cakir B. Cakir - 2 months ago 17
PHP Question

PHP: Multi-Dimensional Array Insert Glitch

So I am trying to create a multi dimensional array that holds the memberID of developers per team.

To clarify, it is supposed to look like this:

$MultiDimensionalArray = array (
$teamArray[$teamID] = array (
[0] => '$memberID',
[1] => '$memberID'
),
$teamArray[$teamID] = array (
[0] => '$memberID',
[1] => '$memberID'
)
)


The problem is, is that when inserting the memberID's, something goes wrong.

This is the first part of the code I use.

$teamIDarray = [];
$developersMultiArray = [];
$developersTeamIDArray = [];

while($preparedStatement->fetch()) {
$teamIDarray[] = $teamID;
}

foreach($teamIDarray as $teamID) {
$developersMultiArray[$teamID] = $developersTeamIDArray;
}


The output of the array is now (keys are teamID's):

developersMulti array(5) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } [178]=> array(0) { } [179]=> array(0) { } }


Great so far, now I want to add the memberID's of the developers in the teamID array. So I add this extra code to the foreach loop from just a second ago. Making it:

foreach($teamIDarray as $teamID) {
$developersMultiArray[$teamID] = $developersTeamIDArray;

while($preparedStatement->fetch()) {
$developersTeamIDArray[] = $developerID;
}
}


It is at this point my array goes full retard.

Complete step by step output:

To clarify again, the code gets all the teams where the logged in user is part of as is seen in the 'teamID array'.
After that it get the members that are designated as developer in the team. 175 and 176 have no developers in their team, so won't be in the array.

teamID array(5) { [0]=> int(175) [1]=> int(176) [2]=> int(177) [3]=> int(178) [4]=> int(179) }

teamID: 177
memberID: 9
//should add memberID 9 to array with 177 as key, but doesn't for some reason.
developersMulti array(3) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } }

teamID: 178
memberID: 9
//for some reason, this goes correct
developersMulti array(4) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } [178]=> array(1) { [0]=> int(9) } }

teamID: 178
memberID: 10
//didn't add memberID 10 to team 178
developersMulti array(4) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } [178]=> array(1) { [0]=> int(9) } }

teamID: 179
memberID: 9
//adds two unknown user, probably user 9 and 10 that got skipped in the previous iterations
developersMulti array(5) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } [178]=> array(1) { [0]=> int(9) } [179]=> array(3) { [0]=> int(9) [1]=> int(9) [2]=> int(10) } }

teamID: 179
memberID: 10
//got skipped again
developersMulti array(5) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } [178]=> array(1) { [0]=> int(9) } [179]=> array(3) { [0]=> int(9) [1]=> int(9) [2]=> int(10) } }

teamID: 179
memberID: 11
//skipped again
developersMulti array(5) { [175]=> array(0) { } [176]=> array(0) { } [177]=> array(0) { } [178]=> array(1) { [0]=> int(9) } [179]=> array(3) { [0]=> int(9) [1]=> int(9) [2]=> int(10) } }


Hope I made everything clear.

Answer

Assuming the fact that with each fetched row/result from your while loop, you're getting the $developerID and the corresponding $teamID, the solution would be like this,

There's no need to create three separate arrays or foreach loop, a simple while loop will do just fine.

$developersMultiArray = [];
while($preparedStatement->fetch()){
    // $developerID corresponds to a particular $teamID
    $developersMultiArray[$teamID][] = $developerID;
}

// display $developersMultiArray array
var_dump($developersMultiArray);
Comments