简体   繁体   中英

Combine arrays with the same id

I am getting data from my database. I have two tables that are relational tables. I am using sql JOIN to get the information needed. Now i have multiple arrays with data in them. But now i would like to combine arrays with the same ID(team_id) and then echo it on the page. Or i mean combine arrays with the same ID(team_id) that i can sort them correctly for each team.

Here is my array that comes from my database. there is can be an infinite number of team names(arrays).

Array
(
    [points_1] => 2
    [0] => 2
    [points_2] => 10
    [1] => 10
    [name] => Team 1
    [2] => Team 1
    [team_id] => 1
    [3] => 1
)
Array
(
    [points_1] => 7
    [0] => 7
    [points_2] => 10
    [1] => 10
    [name] => Team 1
    [2] => Team 1
    [team_id] => 1
    [3] => 1
)
Array
(
    [points_1] => 10
    [0] => 10
    [points_2] => 10
    [1] => 10
    [name] => Team 1
    [2] => Team 1
    [team_id] => 1
    [3] => 1
)
Array
(
    [points_1] => 4
    [0] => 4
    [points_2] => 15
    [1] => 15
    [name] => Team 1
    [2] => Team 1
    [team_id] => 1
    [3] => 1
)
Array
(
    [points_1] => 14
    [0] => 14
    [points_2] => 14
    [1] => 14
    [name] => Team 1
    [2] => Team 1
    [team_id] => 1
    [3] => 1
)
Array
(
    [points_1] => 22
    [0] => 22
    [points_2] => 22
    [1] => 22
    [name] => Team 1
    [2] => Team 1
    [team_id] => 1
    [3] => 1
)
Array
(
    [points_1] => 1
    [0] => 1
    [points_2] => 10
    [1] => 10
    [name] => Team 2
    [2] => Team 2
    [team_id] => 3
    [3] => 3
)
Array
(
    [points_1] => 10
    [0] => 10
    [points_2] => 10
    [1] => 10
    [name] => Team 3
    [2] => Team 3
    [team_id] => 6
    [3] => 6
)

Here is my Foreach / JOIN code:

echo "<pre>";
    foreach ($db->query("SELECT points_1, points_2, name, team_id FROM points INNER JOIN teams ON teams.id=points.team_id ORDER BY team_id ASC") as $result) {
        print_r($result);

    }

I would like go through all the arrays and echo them neatly and sorted like in the picture.

在此处输入图片说明

if you output is like this array(you can set the keys you need)

$arr = array(
array('name' => 'Team 1','points_1' => 2,'points_2' => 10),
array('name' => 'Team 1','points_1' => 7,'points_2' => 10),
array('name' => 'Team 2','points_1' => 1,'points_2' => 12),
array('name' => 'Team 3','points_1' => 4,'points_2' => 32),
array('name' => 'Team 2','points_1' => 1,'points_2' => 16),
);

then try:

$arrayTotals = array();


foreach ($arr as $result) {
        //print_r($result);

        $arrayTotals[$result['name']][] = array('points_1'=>$result['points_1'],'points_2'=>$result['points_2']);

}

var_dump($arrayTotals);

foreach($arrayTotals as $team=>$values){
    echo '<div style="float:left;">'.$team;
    $sum1=0;
    $sum2=0;
    foreach($values as $v){
        echo '<br />'.$v['points_1'].'/'.$v['points_2'];
        $sum1 +=$v['points_1'];
        $sum2 +=$v['points_2'];
    }
    echo '<br />Total:'.$sum1.'/'.$sum2;
    echo '</div>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM