I am implementing a function to calculate date. the function makes the calculations to find each element: year, month, week, day, hour, minute, second.
the result of the calculation is an array like this:
array (
[year] => 0
[month] => 0
[week] => 0
[day] => 0
[hour] => 3
[minute] => 193
[second] => 11583
)
must now remove any occurrence that is KEY = 0, and leave only the indices hour, minute, second.
there's some native PHP function that can do this?
Thank you all
array_filter ( array $input);
Should work. It will delete all elements with value equals to false.
If you would like all elements that are === 0 create your own callback function for array_filter like in the link below.
http://php.net/manual/en/function.array-filter.php
Related
Hi I need to select a rand value in the array removed and short the array i came out whit this small code but it keeps in an infinite loop but this is the weird look
<?php
$array=array("1","2","3","4","5","6","7","8","9","0");
$count=count($array);
for ($il=1;$il<=$count;$il++){
$array_value=array_rand($array, 1);
$array_value_key = array_search($array_value, $array);
$array_key_last=array_key_last($array);
for($if=0;$if<=$array_key_last;$if++){
if ($if==$array_value_key){
for($ia=$array_value_key;$ia<=$array_key_last;$ia++){
if ($ia<$array_key_last){
$ian=$ia+1;
$array[$ia]=$array[$ian];
}else{
unset($array[$ia]);
}
}
}
}
print_r($array);
}
?>
there the output can be different each time likes this but never ends
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 0
)
Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 6
[4] => 7
[5] => 8
[6] => 9
[7] => 0
)
^C
and i have to break it but as you can see in the first loop work as expected removes the number 4 and in the second loop removes the number 1 but don't finish the third loop
I have reviewed many times and get in to the conclusion of the problem it is on the line
$array[$ia]=$array[$ian];
if i add a echo here get printing a number it self to the infinity why?
$array[$ia]=$array[$ian];
echo $array[$ia],"\n";
The Problem is not in one line. It takes a few commands to interact:
array_rand returns the key of the element not the value
array_search returns (bool)false if it doesn't find the value (because of 1 this can happen)
You use $array_value_key to start a for loop. Because of 2 it can be (bool)false.
When you increment a boolean, it does not change. I.e. your $ia++ does nothing.
That's why $ia<=$array_key_last will never turn false and your loop runs forever.
Please sort the below array by its index(date). I want to sort the below array by ASC order by its date. After sorting the first one will be 2020-06-29 and its array, then 2020-06-30, I have tried more sorting mechanism but it could not solve the issue. Please help me to sort out this issue
Array(
[2020-07-01] => Array
(
[0] => Array
(
[slot] => 09:00 AM-11:00 AM
[is_available] => 1
[slot_id] => 29
)
)
[2020-06-29] => Array
(
[0] => Array
(
[slot] => 02:16 AM-02:16 AM
[is_available] => 1
[slot_id] => 14
)
)
[2020-06-30] => Array
(
[0] => Array
(
[slot] => 09:00 AM-06:00 PM
[is_available] => 1
[slot_id] => 15
)
)
[2020-07-02] => Array
(
[0] => Array
(
[slot] => 10:00 AM-05:00 PM
[is_available] => 1
[slot_id] => 35
)
)
[2020-07-03] => Array
(
[0] => Array
(
[slot] => 10:00 AM-03:00 PM
[is_available] => 1
[slot_id] => 36
)
)
)
Try ksort
here is a short example :
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
https://www.php.net/manual/en/function.ksort.php
EDIT :
Since ksort seems to not be working, you should use uksort.
function date_compare($a, $b)
{
$t1 = strtotime($a);
$t2 = strtotime($b);
return $t1 - $t2;
}
uksort($data, 'date_compare');
here is a working example :
https://repl.it/repls/FoolhardyDrabPolygon
Step 1 : Create a function which accepts two dates ( strings ) as parameters, this function returns 1 if first date is greater than the second, it returns -1 if second date > first date or 0 if date's are equal. Working of functions starts from Step 2.
Step 2 : Toeknize the strings to get respective year, month and day of the two dates respectively. [ see : https://www.php.net/manual/en/book.tokenizer.php ]
Step 3 : If first date's year > second date's year, return 1 . If second date's year > first date's year return -1, if they are equal proceed to Step 4.
Step 4 : If first date's month > second date's month, return 1 . If second date's month > first date's month return -1, if they are equal proceed to Step 5.
Step 5 : If first date's day > second date's day, return 1 . If second date's day > first date's day return -1, if they are equal return 0.
Now use php's usort function and pass your function in it
[see : https://www.php.net/usort ]
This is somewhat basic but one of my first PHP projects and I'm stuck. I need to create something that will iterate through a series of DateTime objects and sort them into groups based on the interval of time between each object, with the end goal being to average data associated with these groups of dates (just focusing on how to group the data in this post). If the DateTime objects are more than 1 day apart, then they need to be in a separate sub-group but still a part of the larger list of dates.
I have been able to create DateInterval objects from the DateTime objects. Below is an example of two DateTime objects pulled from a larger list of dates and the corresponding DateInterval generated from the amount of time between the two`
[4] => DateTime Object
(
[date] => 2017-04-21 12:50:53.000000
[timezone_type] => 3
[timezone] => America/Denver
)
[5] => DateTime Object
(
[date] => 2015-04-18 12:50:53.000000
[timezone_type] => 3
[timezone] => America/Denver
)
`[9] => DateInterval Object
(
[y] => 2
[m] => 0
[d] => 3
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 734
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)`
Is there a way I could create some sort of array that holds subgroups of date objects in their own array that are sorted based on the size of their intervals? I illustrated the structure of what I'm thinking below.
Array [
Array [
[date1],
[date2],
[date3]
]
Array [
[date4],
[date5],
]
Array [
[date6]
]
]
First, sort your array of dates (if they aren't already sorted).
sort($dates);
Then keep track of the previous item as you iterate the array. If the number of days in the diff interval exceeds your threshold then increment the key used to define the groups in the result array.
$i = 0;
$previous = null;
foreach ($dates as $date) {
if ($previous && $date->diff($previous)->days > 1) {
$i++;
}
$groups[$i][] = $date;
$previous = $date;
}
This is the code that solved my problem, which was made from a slight modification of #Don'tPanic's code.
$incrementKey = 0;
foreach ($datesIntervalArray as $d) {
if ($d->days > 1) {
$incrementKey++;
}
$groupsOfVisits[$incrementKey][] = $d;
`
I'm currently writing a script that would extract all the dates from a message and convert them to timestamps. PHP's strtotime (similar to Unix's date -c 'some date') would be perfect for this, as it recognizes all kinds of dates, such as:
5pm today
2010-11-15 16:30
Thursday 8:00
However, I'm having trouble finding those dates in the first place. For example, in the following string,
I'll be there for dinner tomorrow at 9:00pm
I need to isolate "tomorrow at 9:00pm", as that's the part that strtotime recognizes.
Is there a regular expression or something similar that would return me all dates that can be parsed by strtotime?
The only thing I can think of is date_parse. A regular expression that matches any format accepted by strtotime would be huge.
An example of date_parse:
$str = "I'll be there for dinner tomorrow at 9:00pm";
$parsed = date_parse($str);
print_r($parsed);
It would output something like this (I removed the unimportant parts from it to make it the result lighter):
Array
(
[year] =>
[month] =>
[day] =>
[hour] => 21 // 9:00pm
[minute] => 0 // 9:00pm
[second] => 0 // 9:00pm
[fraction] => 0
[warning_count] => 1
[is_localtime] => 1
[zone_type] => 2
[zone] => -540
[is_dst] =>
[tz_abbr] => I
[relative] => Array
(
[year] => 0
[month] => 0
[day] => 1 // tomorrow (would be -1 for yesterday, etc.)
[hour] => 0
[minute] => 0
[second] => 0
)
)
Whether this works for you depends primarily on what your input looks like. If you have more than one instance of a date in your input string, it will not work as expected.
This might not be totally efficient, but should work for any date string that consists of up to 5 words in length. I would write the function, but I think you'll get the idea with the comments below...
$words = explode(' ',$original_string);
// Use the array_chunk() function break up this array into 1-word,
// 2-word, 3-word, and 4-word long substrings of the original string
// Reform them back into strings and pass each one through strtodate()
I'm trying to get the difference between two arrays, but with array_diff, array_diff_assoc, or array_diff_key I can't get what I want..
Array 1 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 421001,
5 => 421002,
Array 2 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 431447,
5 => 421001,
6 => 421002,
array_diff = array ()
// empty
jarray_diff_assoc = array (
4 => 431447,
5 => 421001,
6 => 421002,
)
// OK but too much :)
array_diff_key = array(
6 => 421002
)
// nope i don't want that :(
I want 431447, cause it's only one time in the first array and twice in the second.
Regards, Tony
Is that exactly what you want? Only those that occur one time in the first, and two times in the second?
You can basically write your own function for that. Search through the second array, get a list of values that occur two times (or more than once, depending on what it is that you actually want), and then search for those in the first one (this you can do using a built-in PHP function array_intersect).