I need to add data to EACH array within a multidimensional array. Here is my code thus far:
<?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);
Here is what I get:
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
)
What I need however, is each previously created array to include the calculated values, like this:
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
)
)
Where am I going wrong?
Thank you in advance!
You don't need to do any splicing.
do {
$row_rsIdeas['calculated'] = ($row_rsIdeas['monthlysearches'] * 9);
array_push($rsIdeas_array, $row_rsIdeas);
} while ($row_rsIdeas = mysql_fetch_assoc($rsIdeas));
Yes, it's that easy.
-- edit --
If you absolutely must have the columns in a particular order, you can do something like this:
$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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.