Get data from PHP array - php

I have the variable $alldates, which with print_r brings me the following:
Array ( [0] => Array ( [0] => 24. May 2016 08:30 - 17:00 [1] => 7. June 2016 08:30 - 17:00 ) )
I need to show the dates (which can be a lot more than two) in PDF, ordered under eachother like:
24. May 2016 08:30 - 17:00
7. June 2016 08:30 - 17:00
How to get the dates from the array and display them?

foreach ($alldates as $index){
echo $index."<br/>";
}
i'm not sure about your array , alternative way :
foreach ($alldates[0] as $index){
echo $index."<br/>";
}

Try with this:
foreach ($alldates as $date) {
echo $date . '<br />';
}
or here thre is another way
$count = count($alldates);
for ($i = 0; $i < $count; $i++) {
echo $alldates[i] . '<br />';
}

Nested loop can be used if its multi-dimensional array like this :
Array
(
[0] => Array
(
[0] => 24. May 2016 08:30 - 17:00
[1] => 7. June 2016 08:30 - 17:00
)
[1] => Array
(
[0] => 24. May 2016 08:30 - 17:00
[1] => 7. June 2016 08:30 - 17:00
)
)
foreach ($alldates as $s_date){
foreach ($s_date as $n_date){
echo $n_date."<br/>";
}
}

Related

Display multidimensional array in a customize way

I have a multidimensional array and it is like this:
Array
(
[Monday] => Array
(
[open] => 05.00 PM
[close] => 04.00 PM
[state] => 0
)
[Tuesday] => Array
(
[open] =>
[close] =>
[state] => 1
)
[Wednesday] => Array
(
[open] => 03.00 AM
[close] => 06.00 PM
[state] => 0
)
[Thursday] => Array
(
[open] =>
[close] =>
[state] => 1
)
[Friday] => Array
(
[open] => 05.00 PM
[close] => 03.00 PM
[state] => 0
)
[Saturday] => Array
(
[open] => 05.00 PM
[close] => 06.00 PM
[state] => 0
)
[Sunday] => Array
(
[open] =>
[close] =>
[state] => 1
)
)
Using this array I want to create output like this:
Monday - 05.00 PM - 04.00 PM
Tuesday - Closed
Wednesday - 03.00 AM - 06.00 PM
Thursday - Closed
Friday - 05.00 PM - 03.00 PM
Saturday - 05.00 PM - 06.00 PM
Sunday - Closed
I tried it with 2 foreach loops, but I couldn't get it to show the expected output.
foreach ($result as $days => $values) {
echo "$days";
foreach ($values as $k) {
echo " - $k";
}
echo "<br/>";
}
Its output is similar to this:
Monday - 05.00 PM - 04.00 PM - 0
Tuesday - - - 1
Wednesday - 03.00 AM - 06.00 PM - 0
Thursday - - - 1
Friday - 05.00 PM - 03.00 PM - 0
Saturday - 05.00 PM - 06.00 PM - 0
Sunday - - - 1
Can anybody tell me how can I figure this out?
NOTE: if state = 0 it doesn't need to display and state = 1 it should be Closed
Do the foreach loop like that:
foreach ($result as $days => $values) {
echo $days." - ";
echo $values["state"] ? "Closed" : $values["open"]." - ".$values["close"];
echo "<br />";
}
You should check state index exist(1) or not(0) like: if ($values['state'])
Try this one:
foreach ($result as $days => $values) {
echo "$days";
if ($values['state']) {
echo " - Closed";
} else {
echo ": ".$values['open']." - ".$values['close'];
}
echo "<br/>";
}
Do foreach loop with html table to get currect aligned line
$data = "<table >";
foreach($result as $day=>$value) {
if($value["state"]==1) {
$value = "<td colspan='3'> Closed </td>";
} else {
$value = "<td> $value[open] </td><td> - </td><td> $value[close] </td>";;
}
$data .= "<tr><td> $day </td><td> - </td> $value</tr>";
}
$data .= "</table>";
echo $data;

display Array within ul li tags

I have an array that looks like this and I want to display it in a more readable format. I would like the bird name (Gray Hawk etc) and then each listing (which the number of result will vary. This is the code I currently have:
$xml = simplexml_load_file($noteable);
$result = array();
foreach ($xml->result->sighting as $sighting) {
$location = (string) $sighting->{'loc-name'};
$bird = (string) $sighting->{'com-name'};
$howMany = (string) $sighting->{'how-many'};
$obsdt = (string) $sighting->{'obs-dt'};
$thenotedate = $obsdt;
$thenotedate = split('T',$thenotedate);
$thenotedate = $thenotedate[0];
$thenotedate = strftime('%a %b %e at %I:%M %p',strtotime($thenotedate));
ksort($result);
if (!isset($result[$bird])) $result[$bird] = array();
$result[$bird][] = $howMany . ' seen at ' . $location . ' on ' . $thenotedate;
}
print"<pre>";
print_r($result);
print"</pre>";
}
And this is the array
[Gray Hawk] => Array
(
[0] => 1 seen at Florida Canyon--lower on Sun Jun 2 at 04:50 PM
[1] => 1 seen at Madera Canyon--Whitehouse Picnic area on Sat Jun 1 at 07:30 AM
[2] => 1 seen at Florida Canyon--lower on Thu May 30 at 07:56 AM
[3] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:40 AM
[4] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:37 AM
[5] => 1 seen at Madera Canyon--Madera Picnic Area on Tue May 28 at 04:45 PM
[6] => 1 seen at Madera Canyon--Proctor Rd. on Mon May 27 at 09:40 AM
)
[MacGillivray's Warbler] => Array
(
[0] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[1] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[2] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:30 PM
)
try something like this -
foreach ($result as $key=>$value){
//echo Bird Name
echo "<h3>$key</h3>";
//start an unordered list
echo "<ul>";
//echo each sighting
foreach($value as $row){
echo "<li>$row</li>";
}
//close the unordered list
echo "</ul>";
}
when printing out the result don't use print_r($result); use a loop which returns each element in the array! If you need infos on how to display the array as you want to tell us exactly how do you want it to be outputted

PHP, multidimensional arrays: to intersected events with days of the month

I want to build a movie timetable. I have $array1 containing the movie titles and airing dates and times:
array =
0: array =
Title: string = American Beauty
Date: string = 25/09/2012
Time: string = 15:00 - 16:20
1: array =
Title: string = The Godfather
Date: string = 25/09/2012
Time: string = 16:20 - 18:20
2: array =
Title: string = Pulp Fiction
Date: string = 26/09/2012
Time: string = 15:00 - 16:20
And I have $array2 containing the days of the month grouped by Mondays, Tuesday s, Wednesday s, Thursday s and Fridays (no movies during the weekend)
Array
(
[1] => Array
(
[0] => 3
[1] => 10
[2] => 17
[3] => 24
[4] =>
)
[2] => Array
(
[0] => 4
[1] => 11
[2] => 18
[3] => 25
[4] =>
)
[3] => Array
(
[0] => 5
[1] => 12
[2] => 19
[3] => 26
[4] =>
)
[4] => Array
(
[0] => 6
[1] => 13
[2] => 20
[3] => 27
[4] =>
)
[5] => Array
(
[0] => 7
[1] => 14
[2] => 21
[3] => 28
[4] =>
)
)
I need to intersect these two arrays so I can print under day 25 the movie “American Beauty” also under day 25 “The Godfather” and under day 26 “Pulp Fiction”.
Meaning I need to print:
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3 4 5
10 11 12
17 18 19
24 25 26
15:00-16:20 American Beauty 15:00-16:20 Pulp Fiction
16:20-18:20 The Godfather
My tries so far:
foreach( $array1 as $key => $value )
{
$theTime = $value['Time'];
$theTime = explode("/", $theTime );
$days[] = $theTime [0];
$months[] = $theTime [1];
}
So I have all the airing days in array $days but from here I don’t know how to follow or even if this approach is the correct one.
Here's how I get $array2:
$month = 9;
$year = 2012;
for ($i = 1; $i <= 31; $i++)
{
$timestamp = mktime(0, 0, 0, $month , $i, $year);
if (date("n", $timestamp) == $month )
{
$day = date("N", $timestamp);
// Monday 1 to Friday 5
if ($day == 1 OR $day <= 5) {
$array2[$day][] = date("j", $timestamp);
}
}
}
Please help, I’m stuck.
Thanks very much
Ok you will iterate over $array1 and parse the date of the movie.
Then, you will look inside array2 if your day is there
foreach($array1 as $movie){
$movieDate = new DateTime($movie);
//as you indexed $array2 with monday = 1 -> friday = 5 you can use the "w" format
if(isset($array2[$movieDate->format("w"))){
$array[$movieDate->format("j")][] = $movie;
}
}
I made some mutation in your output array, it becomes :
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3=>[] 4 =>[] 5=>[]
10=>[] 11 =>[] 12=>[]
17=>[] 18 =>[] 19=>[]
24 =>[] 25=>[ 26 =>[
15:00-16:20 American Beauty, 15:00-16:20 Pulp Fiction]
16:20-18:20 The Godfather]

Compare todays dayname and time through array?

I have an array like this:
Array
(
[0] => stdClass Object
(
[Start] => 08:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Math
)
[1] => stdClass Object
(
[Start] => 10:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Geography
)
[2] => stdClass Object
(
[Start] => 14:00
[dayName] => Tuesday
[dayID] => 2
[courseName] => Science
)
[3] => stdClass Object
(
[Start] => 10:00
[dayName] => Thursday
[dayID] => 4
[courseName] => Math
)
[4] => stdClass Object
(
[Start] => 18:00
[dayName] => Friday
[dayID] => 5
[courseName] => History
)
)
What I want to do is , I want to compare the daya nd time now to the values in the array. For example lets assume that it is 7:00 am and it is Tuesday. Then I want to get the Object[0]. But if it is 11:00 o'clock then i need to get the Object[2] which starts at 14:00 on Tuesday.
It it is Tuesday and 16:00 o'clock then i need Object[3] .
If it is a weekend then i need the beginning of the week which is Tuesday at 08:00 with the Math Course.
I tried to get that using key => value but I mixed up.
How can I compare the Day and then time and in case there is a Course on that combination just pick it up if not just continue checking.
regards
littleblue
function getObject($array){
$timeNow = date('U'); // time now
$oneHour = $timeNow+3600; // time now + 1 hour
foreach($array as $num => $one){ // loop in each $array
$date = strtotime($one->Start); // convert start time to timestamp
if($date >= $timeNow && $date < $oneHour && date('l', $date) == $one->dayName){ // if all criteria met
return $array[$num]; // return that object
}
}
return array('no data'); // if no criteria is met return no data.
}
$course = getObject($yourArray);
echo $course->courseName;
You can mix the use of DateTime and a simple min search :
$myCourse = null;//initialisation
$myCourseDate =null;
$now = new DateTime;
foreach($array as $course){
//get the date from the datas
$date = DateTime::createFromFormat('l h:i',$course->dayName.' '.course->start);
if($now < $date && ($myCourseDate === null || $myCourseDate > $date)){
$myCourse = $course;
$myCourseDate = $date;
}
}
//at the end of the loop, you've got the expected course

How can i store the values of array_count_values in array of object?

i have array like below which is sorted by array_count_values function,
Array
(
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV] => 3
[Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 2pmADV] => 2
[Session #4: Tues July 12th - Fri July 22nd (9-2:00PM)#1 only: 1:30pmADV] => 2
)
i need to convert this array into array of object like below,
stdClass Object
(
[Itemname] => Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV
[Quantity] => 3
)
stdClass Object
(
[Itemname] => Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 2pmADV
[Quantity] => 2
)
stdClass Object
(
[Itemname] => Session #4: Tues July 12th - Fri July 22nd (9-2:00PM)#1 only: 1:30pmADV
[Quantity] => 2
)
how can i do this?
$arrayOfObjects = array();
foreach ($values as $Itemname => $Quantity) {
$arrayOfObjects[] = (object)compact('Itemname', 'Quantity');
}
Something like this?:
foreach($array as $key => $value){
$object = new stdClass;
$object->Itemname = $key;
$object->Quantity = $value;
print_r($object);
}

Categories