php - putting the array data into separate variables

397

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 calleddatestart and 2nd date in todateend. 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!!!!!

137

Answer

Solution:

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

Answer

Solution:

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

Answer

Solution:

Are the dates in$array1 sorted ascending? If not you should callasort($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
340

Answer

Solution:

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

People are also looking for solutions to the problem: mysql - PHP If Else statement not redirecting correctly?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.