简单的PHP数组 - 如何将结果输入变量?

时间:2014-10-05 20:10:28

标签: php arrays

我试图获取数组的结果并将其移至$variable

这是我目前的代码:

<?php
$total_days_month = date('t'); // total days this month

// add "$currentday" day number to an array until $currentday<=$total_days_month
$days_array = array();
for ($currentday=1; $currentday<=$total_days_month; $currentday++) {
if ($total_days_month > $currentday)
{
array_push($days_array,"'$currentday',");
}
else
{
array_push($days_array,"'$currentday'");
}
}
// now transfer the array content to a $variable

$variable = ????

?>

$variable内容必须为'1', '2', '3', '4' ........ '31'

2 个答案:

答案 0 :(得分:3)

$variable = implode("','", $days_array);

答案 1 :(得分:2)

<?php 
$total_days_month = date('t'); // total days this month

// add "$currentday" day number to an array until $currentday<=$total_days_month
$days_array = array();
for ($currentday=1; $currentday<=$total_days_month; $currentday++) {
 if ($total_days_month > $currentday){
    array_push($days_array,"'$currentday',");
 }else{
    array_push($days_array,"'$currentday',");
 }
}

// now transfer the array content to a $variable

$variable = implode(',', $days_array);