I have the following array:
$avail = array($this->item->extraFields->RoomONEDateAvailable->value, $this->item->extraFields->RoomTWODateAvailable->value, $this->item->extraFields->RoomTHREEDateAvailable->value, $this->item->extraFields->RoomFOURDateAvailable->value, $this->item->extraFields->RoomFIVEDateAvailable->value, $this->item->extraFields->RoomSIXDateAvailable->value, $this->item->extraFields->RoomSEVENDateAvailable->value, $this->item->extraFields->RoomEIGHTDateAvailable->value, $this->item->extraFields->RoomNINEDateAvailable->value, $this->item->extraFields->RoomTENDateAvailable->value);
These are the actual values:
Array ( [0] => Thursday, 01 January 1970 [1] => Thursday, 03 September 2015 [2] => Thursday, 01 January 1970 [3] => Thursday, 01 January 1970 [4] => Thursday, 01 January 1970 [5] => Thursday, 01 January 1970 [6] => Thursday, 01 January 1970 [7] => Thursday, 01 January 1970 [8] => Thursday, 01 January 1970 [9] => Thursday, 01 January 1970 )
I need to remove all the 'Thursday, 01 January 1970' dates from the array, so it only contains valid dates, how can I do this please?
Try:
$avail = array_diff($avail, array('Thursday, 01 January 1970'));
you don't need to rebuild your array, you can remove array entries with unset()[reference].
update: you don't need a reference here, you can work with the corresponding array directly:
foreach($avail as $key => $val)
{
if($val == "Thursday, 01, January 1970")
unset($avail[$key]);
}
var_dump($avail);
this is tested and should work for you.
You can use the array_keys() functions, adding the option to search for a specific value, like this:
array_keys($array, "Thursday, 01 January 1970")
It will return an array of matching index, which you should unset from the original array.
Related
I've got a date like : $date = DateTime::createFromFormat('D d/m', 'Mon 05/02'); but instead of 05 february the datetime returned is DateTime Object ( [date] => 2021-02-08 10:02:10.000000 [timezone_type] => 3 [timezone] => Europe/Brussels )
Answer
Corrected with the Y input and got the right result, php was using 2021 when i was constructing 2022 year
If the (wrong) day of the week is to be ignored, then an * only needs to be set in the format instead of the "D".
$date = DateTime::createFromFormat('* d/m', 'Mon 05/02');
"Mon" is ignored and the expression "05/02" is used to determine the date.
DateTime::__set_state(array(
'date' => "2021-02-05 18:28:31.000000",
'timezone_type' => 3,
'timezone' => "Europe/Berlin",
))
Because in 2021, February 5 is Friday, and February 8 is Monday.
I have created an array of the next 10 days, with a 2 days buffer (i.e. if it is a Monday, the array starts on Wednesday). I am now trying to remove weekends from my array but unsure how to go about doing this. Below is my PHP and the returned array:
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This returns:
Array (
[1548192409] => Tuesday, 22nd Jan
[1548365209] => Thursday, 24th Jan
[1548538009] => Saturday, 26th Jan
[1548710809] => Monday, 28th Jan
[1548883609] => Wednesday, 30th Jan
[1549056409] => Friday, 1st Feb
[1549229209] => Sunday, 3rd Feb
[1549402009] => Tuesday, 5th Feb
[1549574809] => Thursday, 7th Feb
[1549747609] => Saturday, 9th Feb
)
Can somebody help me understand how I would filter out any Saturdays or Sundays from the above
http://php.net/manual/en/function.date.php
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
if (!in_array(date('w',$date_buffer), [0,6])) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
}
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This is a good job for the DatePeriod class. We set up a period of 10 recurrences of 2 days from the start time (in 2 days), and then can iterate through the dates, checking for a weekend day (day of week = 0 or 6) to exclude them from the output:
$start = new DateTime('+2 days');
$period = new DatePeriod($start, new DateInterval('P2D'), 9);
foreach ($period as $date) {
$dow = (int)$date->format('w');
if ($dow != 0 && $dow != 6) {
$days[$date->format('U')] = $date->format('l, jS M');
}
}
print_r($days);
Output:
Array (
[1548194036] => Tuesday, 22nd Jan
[1548366836] => Thursday, 24th Jan
[1548712436] => Monday, 28th Jan
[1548885236] => Wednesday, 30th Jan
[1549058036] => Friday, 1st Feb
[1549403636] => Tuesday, 5th Feb
[1549576436] => Thursday, 7th Feb
)
If you wanted 10 consecutive days (excluding weekends) from 2 days from today, you would just change the second line of the code to:
$period = new DatePeriod($start, new DateInterval('P1D'), 9);
and the output would be:
Array (
[1548197829] => Tuesday, 22nd Jan
[1548284229] => Wednesday, 23rd Jan
[1548370629] => Thursday, 24th Jan
[1548457029] => Friday, 25th Jan
[1548716229] => Monday, 28th Jan
[1548802629] => Tuesday, 29th Jan
[1548889029] => Wednesday, 30th Jan
[1548975429] => Thursday, 31st Jan
)
Demo on 3v4l.org
Here is a simple answer using the while loop.
https://3v4l.org/0lpGX
<?php
$x = 1; // Start
$y = 10; // Iterations Needed
$days = []; //Empty Array
while($x <= $y) {
// Set Buffer
$buffer = 2 + $x;
// Get Date with Buffer
$date = date(strtotime("+$buffer days"));
// If the day is a weeday
if(date('N', $date) < 6){
// Add to array
$days[$date] = date("l, jS M", $date);
// If not, increase max iteration (example: 10 to 11)
}else{
$y++;
}
// Go to next loop
$x++;
}
echo "<pre>";
print_r($days);
?>
Which prints out
Array
(
[1548283397] => Wednesday, 23rd Jan
[1548369797] => Thursday, 24th Jan
[1548456197] => Friday, 25th Jan
[1548715397] => Monday, 28th Jan
[1548801797] => Tuesday, 29th Jan
[1548888197] => Wednesday, 30th Jan
[1548974597] => Thursday, 31st Jan
[1549060997] => Friday, 1st Feb
[1549320197] => Monday, 4th Feb
[1549406597] => Tuesday, 5th Feb
)
Please tell how to make regex code to find all occurrence of date from the string, in the format listed below :
26 jan 2016
26th jan 2016
26 january 2016
26th january 2016
26 feb 15
26th feb 15
ie :
$string = "Test 26 jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
I want the result as an array where i will get :
$res = array (
'2016-01-26',
'2015-02-12',
'2013-01-27'
)
Please tell how to make the same using PHP regex.
The solution using preg_match_all, array_map, date and strtotime functions (I've modified the initial string a bit to get a complex case):
$string = "Test 26th jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
preg_match_all("/\b\d{2}(?:\w{2})? \w+? \d{2,4}\b/i", $string, $matches);
$dates = array_map(function($d) {
return date('Y-m-d', strtotime($d));
}, $matches[0]);
print_r($dates);
The output:
Array
(
[0] => 2016-01-26
[1] => 2015-02-12
[2] => 2013-01-17
)
I've got an array that is an array of arrays, ordered by date, as so:
Array
(
[1379167200] => Array
(
[110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
)
[1380376800] => Array
(
[71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
)
[1381588200] => Array
(
[72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
)
[1382796000] => Array
(
[74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
)
[1384009200] => Array
(
[81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)
)
I want to keep the ordering (i.e., ordered by date), but preferably only include the most inner array items in a single array. Like so:
Array
(
[110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
[71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
[72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
[74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
[81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)
How would this be possible? Would I need to convert the integers to strings or something?
I absolutely need to maintain the ordering by date, but I also need to maintain the relationship between the integer key and the value, both are used.
Iterate through array, create new array:
$newArray = Array();
foreach($array as $row) { // $row is subarray
$value = current($row); // first value in subarray "Itroduction to ..."
$key = key($row); // first key in subarray 101, 71, 72
$newArray[$key] = $value;
}
Demo;
http://codepad.viper-7.com/dd3PLV
Docs:
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.key.php
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);
}