我有 2 个多维数组,可以从数据库中的 2 个不同表中获取数据。其中一个有 3 个团队,另一个有团队的订单。
$sql = "select id, supervizori, grupi, tipo_crt, operatori, data, status, nome, cognome, cel, codice_fiscale from kontrata";
$result = mysqli_query($dbCon, $sql);
if(!$sql) {
die("Error1");
} //this query takes the orders from the kontrata table.
$sql_2 = "select * from grupi";
$result_2 = mysqli_query($dbCon, $sql_2);
if(!$sql_2) {
die("Error2");
} // this query take the teams from the grupi table.
while ($row = mysqli_fetch_assoc($result)) {
$kontratat[$row['id']]['supervizori'] = $row['supervizori'];
$kontratat[$row['id']]['grupi'] = $row['grupi'];
$kontratat[$row['id']]['tipo_crt'] = $row['tipo_crt'];
$kontratat[$row['id']]['operatori'] = $row['operatori'];
$kontratat[$row['id']]['data'] = $row['data'];
$kontratat[$row['id']]['status'] = $row['status'];
$kontratat[$row['id']]['nome'] = $row['nome'];
$kontratat[$row['id']]['cognome'] = $row['cognome'];
$kontratat[$row['id']]['cel'] = $row['cel'];
$kontratat[$row['id']]['codice_fiscale'] = row['codice_fiscale'];
} // In this loop i take the data according to the order table.
while ($row = mysqli_fetch_assoc($result_2)) {
$kontratat_2[$kontratat['id']]['grup_name'] = $row['grup_name'];
} // In this loop i take the teams.
循环后,我使用 foreach 将数据发送到表中,但是当我调用 foreach 结果时,我得到了所有订单(这是正常的),但我有 100 个订单这3支球队已经做到了。我想使用 secont 循环过滤每个团队的订单。简而言之,我使用第一个查询中的"ID"查看每个团队的所有订单,而不是向同一团队支付 50 次。谢谢:)
Array
(
[11] => Array
(
[supervizori] => data
[grupi] => Doctors //this is the team
[tipo_crt] => LUCE
[operatori] => data
[data] => 2015-12-20
[status] => OK
[nome] => data
[cognome] => data
[cel] => data
[codice_fiscale] => shkrfl93a08a123a
)
[12] => Array
(
[supervizori] => data
[grupi] => BMW //this is the team
[tipo_crt] => data
[operatori] => data
[data] => 2015-12-22
[status] => KO
[nome] => rgrg
[cognome] => grdgdrgdrg
[cel] => serges
[codice_fiscale] => a
)
[17] => Array
(
[supervizori] => data
[grupi] => Doctors //this is the team
[tipo_crt] => GAS
[operatori] => data
[data] => 2015-12-24
[status] => Atessa
[nome] => dscc
[cognome] => csdfvsg
[cel] => wgwegwe
[codice_fiscale] => rgrwgrwg
)
[18] => Array
(
[supervizori] => data
[grupi] => Bosat //this is the team
[tipo_crt] => data
[operatori] => data
[data] => 2015-12-24
[status] => OK
[nome] => dscc
[cognome] => csdfvsg
[cel] => wgwegwe
[codice_fiscale] => rgrwgrwg
)
[19] => Array
(
[supervizori] => data
[grupi] => Doctors //this is the team
[tipo_crt] => LUCE
[operatori] => data
[data] => 2015-12-29
[status] => Atessa
[nome] => dhr
[cognome] => rhrdh
[cel] => rdhrdh
[codice_fiscale] => rdhrdh
)
[20] => Array
(
[supervizori] => data
[grupi] => Doctors //this is the team
[tipo_crt] => LUCE
[operatori] => data
[data] => 2016-01-04
[status] => OK
[nome] => dwadwa
[cognome] => jgv
[cel] => jlgv
[codice_fiscale] => jgv
) // this is the first array from orders
Array
(
[8] => Array
(
[grup_name] => BMW
)
[9] => Array
(
[grup_name] => Doctors
)
[10] => Array
(
[grup_name] => Bosat
)
)// the teams array
这应该可以完成工作:
$completeData = array();
foreach($orders as $order)
{
if(!array_key_exists($order['grupi'], $completeData))
{
$completeData[$order['grupi']] = array();
}
$completeData[$order['grupi']][] = $order;
}
根据您的需要进行调整。