0
我需要將數據添加到多維數組中的每個數組。這裏是我的代碼至今:如何在PHP中將數據從while循環拼接到多維數組中?
<?php
//Arrays
$rsIdeas_array = array();
//Query Database
mysql_select_db($database_connFormula, $connFormula);
$query_rsIdeas = "SELECT * FROM ideas";
$rsIdeas = mysql_query($query_rsIdeas, $connFormula) or die(mysql_error());
$row_rsIdeas = mysql_fetch_assoc($rsIdeas);
$totalRows_rsIdeas = mysql_num_rows($rsIdeas);
//loop bizideas into array
do {
$calculated = ($row_rsIdeas['monthlysearches'] * 9);
array_push($rsIdeas_array, $row_rsIdeas);
array_splice($rsIdeas_array, 7, 0, $calculated);
} while ($row_rsIdeas = mysql_fetch_assoc($rsIdeas));
print_r($rsIdeas_array);
這裏是我得到:
Array
(
[0] => Array
(
[ideaID] => 1
[userID] => 1
[bizidea] => Business Idea 1
[bizexplained] => Business Idea 1 Explanation
[bizmodel] => Utility
[repkeyword] => Keyword 1
[monthlysearches] => 33100
[advcomp] => 0.95
[startease] => 6
)
[1] => 297900
[2] => Array
(
[ideaID] => 2
[userID] => 1
[bizidea] => Business Idea 2
[bizexplained] => Business Idea 2 Explained
[bizmodel] => Service
[repkeyword] => Keyword 2
[monthlysearches] => 6600
[advcomp] => 0.93
[startease] => 8
)
[3] => 59400
)
什麼,但是我需要的,是每個先前創建的陣列,包括計算的值,如:
Array
(
[0] => Array
(
[ideaID] => 1
[userID] => 1
[bizidea] => Business Idea 1
[bizexplained] => Business Idea 1 Explanation
[bizmodel] => Utility
[repkeyword] => Keyword 1
[monthlysearches] => 33100
[calculated] => 297900 //Here is where I need the calculated values
[advcomp] => 0.95
[startease] => 6
)
[1] => Array
(
[ideaID] => 2
[userID] => 1
[bizidea] => Business Idea 2
[bizexplained] => Business Idea 2 Explained
[bizmodel] => Service
[repkeyword] => Keyword 2
[monthlysearches] => 6600
[calculated] => 59400 //Here is where I need the calculated values
[advcomp] => 0.93
[startease] => 8
)
)
我哪裏錯了?
預先感謝您!
真棒,這是正在努力添加到數組的末尾。但是,你會知道如何在[monthlysearches]之後立即[計算]? – KevBot 2013-02-09 01:30:44
定製關聯數組中列的順序並不是一個很好的方法 - 你不管是對整個事物進行排序還是重新聲明數組。 – 2013-02-09 01:33:42
我添加了幾個選項,如果這就是你真正想要做的 – 2013-02-09 01:40:06