Jump to content

returning the result of multidimentional array


Go to solution Solved by Barand,

Recommended Posts

Hi All,

I have a php function that returns a list of players and which team they play for.

I am looking to create an array that at the top level has the teams and then second level will be first and last name of the player.

i think i have done this like this

$stmt -> bind_result($fn, $ln, $team);
	$squad = [];
	while($stmt -> fetch()){
		$squad[$team] .= [$fn, $ln];
	}

Then i am wanting to return the output to my page as a list (for ease of the example) with the team at the top and all of the player underneath:

Team 1

Player 1

Player 2

Team 2

Player 3

Player 4

	foreach($squad as $team){
		$out .= "$team";

		foreach($team as $key => $val){
			$out.="$key $value";
		};
	};

I thought that the above could help me but i am getting a bit lost.

  • Solution

(This example uses the "house" and "pupil" tables from my SQL tutorials instead of your teams and players)

$res = $db->query("SELECT
                          h.house_name
                        , p.fname
                        , p.lname
                   FROM house h 
                        JOIN pupil p USING (houseid)       
                  ");
$data = [];
foreach ($res as $row) {
    if (!isset($data[$row['house_name']])) {
        $data[$row['house_name']] = [];
    }
    $data[$row['house_name']][] = [$row['fname'], $row['lname']];
}

foreach ($data as $house => $pupils)  {
    echo "$house<ul>";
    foreach ($pupils as $p) {
        echo "<li>{$p[0]} {$p[1]}</li>";
    }
    echo "</ul>";
}

giving

image.png.489ab4eaf1eae4805cf399d7099cad5a.png

I do have one little issue with this.

I have the following

foreach ($res as $row) {
		if (!isset($data[$row['house_name']])) {
			$data[$row['house_name']] = [];
		}
		$data[$row['name']][] = [$row['id'], $row['first_name'], $row['last_name']];
	}
		
	foreach ($data as $team => $players) {
		
			$out .= "<div class='col-4'>
					<h5>$team</h5>
				";
		
		
		foreach ($players as $p) {
			$out.= "<input class='form-control w-100 mb-3' type='text' disabled='disabled' name='' data-value='$p[0]' value='$p[1] $p[2]'>";
		}
		$out .= "</div>";
	}

	return $out;

and i am getting a rougue div created before the 2 divs that i am expecting - you can see the 3 col-4s

483624238_Screenshot2022-01-03at17_54_32.png.4da3a289fc072f113d29ff85eb870f0d.png

 

The first one is always blank.  I am assuming that this is something simple but i am struggling to see what it is.

The code you posted above outputs this for me (changed only by adjusting newlines and tabs)

<div class="col-4">
    <h5>Laker</h5>
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="1" value="Adam Simms">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="7" value="David Powell">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="9" value="George Wilson">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="11" value="Jane Morrison">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="21" value="Peter Adamson">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="23" value="Wayne Jones">
</div>
<div class="col-4">
    <h5>Grace</h5>
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="2" value="Allan Blair">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="5" value="Anthony Bell">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="6" value="Caroline Freeman">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="14" value="John Watson">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="15" value="Jack Williams">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="19" value="Mary Whitehouse">
</div>
<div class="col-4">
    <h5>Jardine</h5>
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="4" value="Anne Bailey">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="12" value="John Patterson">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="13" value="John Tully">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="18" value="Mary Sheldon">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="20" value="Michael Grove">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="22" value="Peter Appleby">
</div>
<div class="col-4">
    <h5>Cowdrey</h5>
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="3" value="Anna Hamilton">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="8" value="Emma Watson">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="10" value="Henry Irving">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="16" value="Margaret Norton">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="17" value="Mary Blake">
    <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="24" value="William Smith">
</div>

No col-6s and nothing rogue.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.