Log in

View Full Version : How to push array like this?



smansakra
01-11-2011, 02:15 AM
$day_array = array();
foreach (range(31, 1)as $day){
array_push($day_array, $day);
}


if i write a code like above, the $day_array will return like this:
Array (
[0] => 31
[1] => 30
[2] => 29
[3] => 28
and so on...

is there a solution to make 'my array key' and 'my key value' has the same value like this one
$day_array = array(
'31' => '31',
'30' => '30',
'29' => '29'
and so on...
);

thanks.. i need help...

traq
01-11-2011, 02:28 AM
$day_array = array();
foreach (range(31, 1)as $day){
$day_array[$day] = $day;
}

smansakra
01-11-2011, 03:11 AM
Thanks so much, i will try it...

cindylou
01-11-2011, 07:01 AM
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>


This is possible only if iterated array can be referenced (i.e. is variable), that means the following code won't work:

<?php
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}

?>