Reputation: 37
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 |
-+--------------------------------------+-
Upvotes: 0
Views: 76
Reputation: 1216
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;
Upvotes: 1
Reputation: 2837
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.
Upvotes: 2