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.

 

 

 

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
)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.