![](/img/trans.png)
[英]PHP populate multi dimensional array using 2 methods and loop, 1 depending on the other
[英]Populate multi dimensional array in PHP
您好,我从我的 MySQL 数据库中获得了一些记录。 我必须像这样填充多维关联数组:
results{
"key#1": array
array
array
"key#2": array
...
}
我使用以下代码来创建我的数据结构,但我不知道如何在我的关联多维数组中推送数据。
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$nome_paziente = $row[0];
$numero_richiesta = $row[1];
$importo_manuale = $row[2];
$sconto_totale = $row[3] + $row[4];
$importo_finale = round(($row[5] - (($row[5] * $sconto_totale)/100)),2);
$id_centro_operativo = $row[7];
$descrizione = $row[9];
$codice_convenzione = $row[10];
$elements = array(
'nome_paziente' => $nome_paziente,
'numero_richiesta' => $numero_richiesta,
'importo_manuale' => $importo_manuale,
'importo_finale' => $importo_finale,
'descrizione' => $descrizione,
'codice_convenzione' => $codice_convenzione
);
$key = $row[8]."#sep#".$row[6]; //nome_studio#data_appuntamento
$results[$key] = $elements;
}
var_dump($results);
每个键我只有一个数组。
如果$result
中不存在$key
则每次使用 make $results[$key]
作为数组时都会覆盖值$results[$key]
否则将$elements
附加到$results[$key]
$key = $row[8]."#sep#".$row[6]; //nome_studio#data_appuntamento
if (array_key_exists($key,$results)) {
$results[$key][] = $elements;
}
else{
$results[$key] = array();
$results[$key][] = $elements;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.