0

I have this array output:

[0] => Array
    (
        [date] => 2014-04-02
        [0] => 2014-04-02
        [shiftName] => Long Day
        [1] => Long Day
    )

[1] => Array
    (
        [date] => 2014-04-03
        [0] => 2014-04-03
        [shiftName] => Long Day
        [1] => Long Day
    )

[2] => Array
    (
        [date] => 2014-04-04
        [0] => 2014-04-04
        [shiftName] => Long Day
        [1] => Long Day
    )

Is it possible to set a data into a variable?

For example:

$date = 2014-04-06;
$shiftname = Long Day;

and if so, from the results how do I make it into a table like this using a loop?:

-+--------------------------------------+- 
 | 2014-04-02 | 2014-04-03 | 2014-04-06 |
-+--------------------------------------+-
 | Long Day   | Long Day   | Long Day   |
-+--------------------------------------+-
1
  • It is possible, but you would only be able to have one of the three sets of data if you did that - just access the data via $array[index]["date"]? Commented Apr 2, 2014 at 0:32

2 Answers 2

2

http://ua.php.net/extract should be useful for you. It does exactly what you describe.

There may be some caveats with multiple arrays being present with the same keys. But that brings up the point that automation of variable assignment inevitably has drawbacks; it's good to be relatively specific about assignments. If you need each array to be extracted, you're going to have to iterate through and give prefixes to the keys or something to avoid collisions.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want extract information from the Array into a single variable just do it this way:

$date = $your_array[0]['date'];
$shiftname = $your_array[0]['shiftName'];

If you want to save information into the array do it this way:

$your_array[0]['date'] = $date;
$your_array[0]['shiftName'] = $shiftname;

In order to display it as a table, try this:

// Create an empty Array for the dates and one for the shift names
$dates = array(); $shiftNames = array();
// Initiate the table string
$table = "<table>";

// Save each date and shift from your original array into the date or shiftName array    
foreach($your_array as $sub_array) {
    $dates[] = $sub_array['date'];
    $shiftNames[] = $sub_array['shiftName'];
}

// Create a new row in your table
$table .= "<tr>";

// for each date insert a cell into your table and the first row
foreach($dates as $date) {
   $table .= "<td>".$date."</td>";
}

// close the first row open a new row
$table .= "</tr><tr>";

// for each shift name insert a cell into the table and the second row with the shfitName
foreach($shiftNames as $shiftName) {
    $table .= "<td>".$shiftName."</td>";
}

// close the second row and close the table
$table .= "</tr></table>";

// Echo the table to where ever you want it to have
echo $table;

5 Comments

Wow thanks for the reply, that solves it but what if I want to loop it?
If you want to print it into a table, as described in your question, I have edited my post above.
Thanks for the help, the above code works for array [1], what about [2] and [3]?
Just store all three arrays in one array (I referred to as $your_array), than the foreach{} loop will manage it. Otherwise please specify the constellation of your arrays.
I tried again and it works. Finally many thanks for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.