user2316844
user2316844

Reputation: 37

putting the array data into separate variables

I am trying to put the array data into two separate variables. But not getting it. Here is my code

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

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

Now i am getting the result :

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

I want to put 1st date in a variable called datestart and 2nd date in to dateend. How can I achieve this?

output for 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)

EDITED HERE (ADDING)

 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];

    }


                    }

I echoed like this and got the o/p

2013-05-20
2013-05-30

It works!!!!!

Upvotes: 1

Views: 207

Answers (4)

Mr. Alien
Mr. Alien

Reputation: 157334

There's no need to loop, if you've an array like

$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

Update : Your array is a nested array, you need to use this

<?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];
?>

Upvotes: 2

antony
antony

Reputation: 2893

Are the dates in $array1 sorted ascending? If not you should call asort($array) to sort it low-to-high.

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

Upvotes: 2

qisho
qisho

Reputation: 364

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

Upvotes: 1

ChristopheBrun
ChristopheBrun

Reputation: 1207

If you're sure $array1 has two rows and that ones are the dates you need, you may look at the list builtin function :

list($start_date, $end_date) = $array1;

Upvotes: 2

Related Questions