AnalyzeThis Posted October 19, 2012 Share Posted October 19, 2012 Hi, I am trying to insert a custom record into a assocative array made from a mysql_fetch_assoc. The amount of rows differs depending on what is fetched sometimes there is very few. I need to insert a row into the fetch where there is none. I.e. there are no records for Oct or Dec so we need to make some up and place them into the array. So I was taking a array from it, i.e. Nov and then trying to modify that array to be "Jan." and all "0"s and then array_splice($records, $count, 0, $gen) it back into the array where needed; which is working fine. What I am having trouble with is changing the values within the one line that I took to insert back after modified. +-------+----------+------- Month | bla | bla | etc.. +-------+----------+------- | Oct | 0 | 0 | Nov | 1 | 0 | Dec | 0 | 0 +-------+----------+------- The assocative array print_r looks something like: gen = Array ( [0] => Array ( [0] => January [Month] => January [1] => 2012 [Year] => 2012 [2] => 0.103813 [topping] => 0.103813 [3] => 91.52665 [topping1] => 91.52665 [4] => 33.31605 [topping2] => 33.31605) How would I go about changing all the fields in the array to "0" and the Month field to "xxx"? I am having trouble working with this data type due to its structure. Quote Link to comment https://forums.phpfreaks.com/topic/269697-after-mysql_fetch_assoc-to-assocative-array-inserting-record/ Share on other sites More sharing options...
AnalyzeThis Posted October 19, 2012 Author Share Posted October 19, 2012 Thanks in advance!? Quote Link to comment https://forums.phpfreaks.com/topic/269697-after-mysql_fetch_assoc-to-assocative-array-inserting-record/#findComment-1386444 Share on other sites More sharing options...
AnalyzeThis Posted October 19, 2012 Author Share Posted October 19, 2012 Still need to optmize it but I figured it out. $i = 0; $count = 0; $gen = array($records[0]); $temp = array_keys($gen[0]); for($x=0; $x<count($temp); $x++){ $gen[0][$temp[$x]] = NULL; } while ($mydate < $timespan["end"]){ $mydate = strtotime ("+$i month", $timespan["start"]); if ($records[$count]["Month"] == date("F", $mydate)){ $count++; } else { $gen[0][0] = date("F", $mydate); array_splice($records, $count, 0, $gen); } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/269697-after-mysql_fetch_assoc-to-assocative-array-inserting-record/#findComment-1386460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.