2013-02-09 90 views
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 
    ) 
) 

我哪里错了?

预先感谢您!

回答

1

你不需要做任何拼接。

do { 
    $row_rsIdeas['calculated'] = ($row_rsIdeas['monthlysearches'] * 9); 
    array_push($rsIdeas_array, $row_rsIdeas); 
} while ($row_rsIdeas = mysql_fetch_assoc($rsIdeas)); 

是的,就这么简单。

- 编辑 -

如果你绝对必须按照特定的顺序列,你可以做这样的事情:

$row_rsIdeas['calculated'] = ($row_rsIdeas['monthlysearches'] * 9); 
$row_rsIdeas = array(
    'ideaID' => $row_rsIdeas['ideaId'], 
    'userID' => $row_rsIdeas['userId'], 
    'bizidea' => $row_rsIdeas['bizidea'], 
    // and so on 
); 
array_push($rsIdeas_array, $row_rsIdeas); 

OR

$row_rsIdeas['calculated'] = ($row_rsIdeas['monthlysearches'] * 9); 
uksort($row_rsIdeas, function($col1, $col2)) { 
    $cols = array('ideaID' => 0, 'userID' => 1, 'bizidea' => 2, ...); //etc 
    return ($cols[$col1] > $cols[$col2]); // this might be backwards, trying reversing 
} 
array_push($rsIdeas_array, $row_rsIdeas); 
+0

真棒,这是正在努力添加到数组的末尾。但是,你会知道如何在[monthlysearches]之后立即[计算]? – KevBot 2013-02-09 01:30:44

+0

定制关联数组中列的顺序并不是一个很好的方法 - 你不管是对整个事物进行排序还是重新声明数组。 – 2013-02-09 01:33:42

+0

我添加了几个选项,如果这就是你真正想要做的 – 2013-02-09 01:40:06