Jump to content

[SOLVED] how to push key=>value(3=>'March') in an array.


zohab

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

try...

<?php
   $firstquarter = array(1 => 'January', 2=>'February',4=>'April');//3=>'March'
   $firstquarter[3] = "March";
   ksort($firstquarter);
   print_r($firstquarter);
?> 

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

$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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.