下面的代碼給出了下面的數組,但是我需要它的格式不同(在數組之後聲明),所以它對於我已經編寫的javascript函數有效。在PHP中更改三維數組中的第一個數組的鍵
$sql = "SELECT towhich, duedate, amount FROM sales WHERE email = '$email' ORDER BY duedate ASC";
$result = mysqli_query($conn, $sql);
$dbarray = array();
while($row = mysqli_fetch_assoc($result)) {
$dbarray[] = $row;
}
$graph = array();
$cnt = 0;
foreach($dbarray as $key => $values){
$orderdate = explode('-', $values['duedate']);
$month = $orderdate[1];
$graph[$month][$cnt] = array (
0 => $values['amount'],
1 => $values['towhich']
);
$cnt ++;
}
//Now it's grouped by date
陣列輸出:
array(5)
{
["02"]=> array(2)
{
[0]=> array(2) { [0]=> string(2) "10" [1]=> string(9) "the co op" }
[1]=> array(2) { [0]=> string(2) "30" [1]=> string(9) "the co op" }
}
["03"]=> array(1)
{
[2]=> array(2) { [0]=> string(2) "50" [1]=> string(9) "the co op" }
}
["04"]=> array(1)
{
[3]=> array(2) {[0]=> string(2) "40" [1]=> string(9) "the co op" }
}
["05"]=> array(2)
{
[4]=> array(2) {[0]=> string(2) "10" [1]=> string(9) "the co op" }
[5]=> array(2) { [0]=> string(2) "10" [1]=> string(9) "the co op" }
}
["06"]=> array(1)
{
[6]=> array(2) { [0]=> string(2) "10" [1]=> string(9) "the co op" }
}
}
數組密鑰索引值不應該是,例如,[ '02'],但是,作爲第一含陣列中,[0],像正常一樣;和'03'應該是[1]。
我查了很多,的確幫助了我生成的代碼,但是所有的答案似乎都是在更改大型數組內部的鍵值。我是新的多維數組,順便說一句。提前致謝。
如果你想知道爲什麼我這樣做,到目前爲止,這是因爲每個第一個數組應該對應不同的月份;這就是爲什麼我按日期和所有這些命令。
'$ graph = array_values($ graph);' – jeroen
_and'03'should be [1] ._ is not clear。你是否希望值「03」作爲每個數組的一部分,包含數量和數量? –
不,03對應於第一行子行,而不是子子陣列,即數量和位置。 – user1849962