1

我正在尝试将数组数据放入两个单独的变量中。但没有得到它。这是我的代码

if($option_query->row['type'] == 'date') {
    $array1[0] = $option_value;

    foreach ($array1 as $key => $value) {
        echo "$key = $value<br>";
    }

现在我得到了结果:

0 = 2013-05-05
0 = 2013-05-07 

我想将第一个日期放入名为的变量datestart中,将第二个日期放入dateend. 我怎样才能做到这一点?

var_dump(array1) 的输出;

array (size=1)
  0 => string '2013-05-05' (length=10)
array (size=2)
  0 => string '2013-05-05' (length=10)
  1 => string '2013-05-07' (length=10)

在这里编辑(添加)

 if($option_query->row['type'] == 'date' )

                        {


$arr = array( //Assuming that this should be the map of your array
        array(
            $option_value
        ),
        array(
            $option_value
            //$option_value
        )
    );

   // var_dump($arr);

    echo $arr[1][0];
   echo $arr[1][1];

    }


                    }

我像这样回应并得到了o / p

2013-05-20
2013-05-30

有用!!!!!

4

4 回答 4

2

没有必要循环,如果你有一个像

$arr = array('1/2/2010', '2/2/2012');

$start_date = $arr[0]; //Assigning 1st index to this var
$end_date = $arr[1]; //Assigning 2nd index to this var

更新:你的数组是一个嵌套数组,你需要使用这个

<?php
    $arr = array( //Assuming that this should be the map of your array
        array(
            '2013-05-05'
        ),
        array(
            '2013-05-05',
            '2013-05-07'
        )
    );

    var_dump($arr);

    echo $arr[1][0];
    echo $arr[1][1];
?>
于 2013-05-04T10:30:42.167 回答
2

如果您确定$array1有两行并且那些是您需要的日期,您可以查看list内置函数:

list($start_date, $end_date) = $array1;
于 2013-05-04T10:34:49.770 回答
2

日期是否$array1按升序排列?如果不是,您应该调用asort($array)从低到高对其进行排序。

if ($option_query->row['type'] == 'date') {
    $dates = $array1; // Keep $array1 in tact
    $datestart = array_shift($dates); // Shifts an element off the front of $dates 
    $dateend = array_pop($dates); // Pops an element off the end of $dates
于 2013-05-04T10:47:27.067 回答
1
$array['datestart'] = $option_value_start; 
$array['dateend'] = $option_value_end;
foreach ($array AS $k=>$v){
     $$k=$v;
}
echo $datestart; // print $option_value_start VALUE
echo $dateend;
于 2013-05-04T10:36:29.060 回答