zohab Posted June 2, 2009 Share Posted June 2, 2009 I have array name "firstquarter" , i want to push key=>value(3=>'March') in an array. How can i implement it. Any idea? <?php $firstquarter = array(1 => 'January', 2=>'February',4=>'April');//3=>'March' print_r($firstquarter); ?> Array ( [1] => January [2] => February [4] => April ) I want array like Array ( [1] => January [2] => February [3] => March [4] => April ) array_push function will push value in an array not key. Quote Link to comment https://forums.phpfreaks.com/topic/160599-solved-how-to-push-keyvalue3march-in-an-array/ Share on other sites More sharing options...
BK87 Posted June 2, 2009 Share Posted June 2, 2009 try... <?php $firstquarter = array(1 => 'January', 2=>'February',4=>'April');//3=>'March' $firstquarter[3] = "March"; ksort($firstquarter); print_r($firstquarter); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160599-solved-how-to-push-keyvalue3march-in-an-array/#findComment-847569 Share on other sites More sharing options...
zohab Posted June 5, 2009 Author Share Posted June 5, 2009 hi , I have string as name1,value1,name2,value2,name3,value3,name4,value4. I wrote following code but does not produce required result. $array=array(); $string="name1,value1,name2,value2,name3,value3,name4,value4"; $explode=explode(",",$string); for($i=0;$i<count($explode);$i++) { $array[$string[$i]]=$array[$string[$i+1]]; } echo"<pre>"; print_r($array); echo "<pre>"; I need following required result. Array ( [name1] => value1 [name2] => value2 [name3] => value3 [name4] => value4 ) Quote Link to comment https://forums.phpfreaks.com/topic/160599-solved-how-to-push-keyvalue3march-in-an-array/#findComment-849891 Share on other sites More sharing options...
Mark Baker Posted June 5, 2009 Share Posted June 5, 2009 $array=array(); $string="name1,value1,name2,value2,name3,value3,name4,value4"; $explode=explode(",",$string); for($i=0;$i<count($explode);$i++) { $array[$explode[$i]] = $explode[++$i]; } echo"<pre>"; print_r($array); echo "<pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/160599-solved-how-to-push-keyvalue3march-in-an-array/#findComment-849898 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.