I have the LatestValuesDate 'Apr','May','Jun'.I have array with no of days for the months.
If LatestValuesDate is may I want to show the number of days =61. if LatestValuesDate is Jul I want to show the number of days =91
but now I got only 31
$month_days=array("Apr"=>"30", "May"=>"31", "Jun"=>"30", "Jul"=>"31", "Aug"=>"31", "Sep"=>"30", "Oct"=>"31", "Nov"=>"30", "Dec"=>"31", "Jan"=>"31", "Feb"=>"28", "Mar"=>"30");
$val='May';
$days1=0;
$noOfDays=$days1+$month_days[$val];
For your expected result, you would need to loop through the array and sum the values into a variable. But you will have to stop the loop after adding the value according to your $val variable. You can achieve that using the following code:
$month_days=array("Apr"=>"30", "May"=>"31", "Jun"=>"30", "Jul"=>"31", "Aug"=>"31", "Sep"=>"30", "Oct"=>"31", "Nov"=>"30", "Dec"=>"31", "Jan"=>"31", "Feb"=>"28", "Mar"=>"30");
$noOfDays = 0;
$val='May';
foreach($month_days as $key=>$value){
$noOfDays = $noOfDays + $value;
if($key == $val)
break;
}
echo $noOfDays;
Just if you don't want to use foreach():
$month_days=array("Apr"=>"30", "May"=>"31", "Jun"=>"30", "Jul"=>"31", "Aug"=>"31", "Sep"=>"30", "Oct"=>"31", "Nov"=>"30", "Dec"=>"31", "Jan"=>"31", "Feb"=>"28", "Mar"=>"30");
$val='May';
$noOfDay = 0;
$month_index = array_search($val, array_keys($month_days));
$noOfDay = array_sum(array_slice($month_days,0,$month_index +1));
var_dump($noOfDay);
Output: int(61)
NOTE: March has the wrong number of days in your array declaration, it should be 31.
I was always advised to not use break like commands in loops and to always have a "well defined loop" that doesn't require a break.
This uses a while loop and a flag. If the search is not found, the loop will end summing all values.
$month_days = ['Apr'=>'30', 'May'=>'31', 'Jun'=>'30', 'Jul'=>'31', 'Aug'=>'31', 'Sep'=>'30', 'Oct'=>'31', 'Nov'=>'30', 'Dec'=>'31', 'Jan'=>'31', 'Feb'=>'28', 'Mar'=>'31'];
$val = 'May';
$day_count = 0;
$i = 0;
$keys = array_keys($month_days);
$done = 0;
while (!$done)
{
$done = $keys[$i] == $val || $i >= count($month_days) ? 1 : 0;
$day_count += $month_days[$keys[$i]];
$i++;
}
echo 'Day Count: ' . $day_count . "\n";
echo 'i: ' . $i . "\n";
Related
i have this function:
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
for ($i = 0; $i < $n; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
// this echos 10,0,40 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
The problem are that im not expecting this output
becouse first number of array (20) is my commission
and the other numbers are my parents commission (10,10,50).
So basically i need to output like this: (0,0,30)
becouse i take 20 of commission,
first parent not take nothing becouse are less of my commission (10)
second parent not take nothing becouse are less of my commission (10)
and only last parent take 30 becouse are greater than my commission (50 - 20 my commission).
Thanks in advance
Since your first element of the array is your commission and the others are the the commissions of parents, and since it seems that you don't want to include your commission in the result array, you can do something like this:
function pairwiseDifference($arry)
{
$n = count($arry);
$diff = 0;
$result = array();
$myComm = $arry[0]; // your commision
for ($i = 1; $i < $n; $i++)
{
$diff = 0; // set $diff to 0 as default
if($myComm < $arry[$i]) // if your commision < parent commision
$diff = $arry[$i] - $myComm;
echo $diff." ";
array_push($result, $diff);
}
return $result;
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
$diffArray = pairwiseDifference($arry);
echo $diffArray[0]; // prints 0
echo $diffArray[1]; // prints 0
echo $diffArray[2]; // prinst 30
To tweak the logic according to your code, there would be only 3 modifications.
Create a $max variable and assign it the value of $arry[0].
Make difference as 0 if current max is greater than current one, else take the difference.
Calculate the new max again using max() function.
Snippet:
<?php
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
$max = $arry[0]; // new addition
for ($i = 1; $i <= $n; $i++) // new modification <= n instead of < n
{
// absolute difference between
// consecutive numbers
$diff = $max < $arry[$i] ? $arry[$i] - $max : 0; // new modification
$max = max($max, $arry[$i]); // new modification
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
// this echos 10,0,40 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
Say I have an array [10000,5000,1000,1000] and I would like to find the closest sum of numbers to a given number. Sorry for the bad explanation but here's an example:
Say I have an array [10000,5000,1000,1000] I want to find the closest numbers to, say 6000.
Then the method should return 5000 and 1000
another example : we want the closest to 14000 , so then he should return 10000 and 5000
I've tried with code below, here is working one but if the $desiredSum and $numbers array is big. it's running so slow until php execution timeout
$numbers = array(
10000,5000,1000,1000
);
$desiredSum = 6000;
$minDist = null;
$minDist_I = null;
// Iterate on every possible combination
$maxI = pow(2,sizeof($numbers));
for($i=0;$i<$maxI;$i++) {
if(!(($i+1) % 1000)) echo ".";
// Figure out which numbers to select in this
$sum = 0;
for($j=0;$j<sizeof($numbers);$j++) {
if($i & (1 << $j)) {
$sum += $numbers[$j];
}
}
$diff = abs($sum - $desiredSum);
if($minDist_I === null || $diff < $minDist) {
$minDist_I = $i;
$minDist = $diff;
}
if($diff == 0) break;
}
$chosen = array();
for($j=0;$j<sizeof($numbers);$j++) {
if($minDist_I & (1 << $j)) $chosen[] = $numbers[$j];
}
echo "\nThese numbers sum to " . array_sum($chosen) . " (closest to $desiredSum): ";
echo implode(", ", $chosen);
echo "\n";
Anyone can help me out ?
<?php
function coinChange($numbers,$desiredSum){
sort($numbers);
$set = [];
$set[0] = [];
for($i = $numbers[0];$i <= $desiredSum;++$i){
foreach($numbers as $index => $current_number){
if($i >= $current_number && isset($set[$i - $current_number])){
if(isset($set[$i - $current_number][$index])) continue;
$set[$i] = $set[$i - $current_number];
$set[$i][$index] = true;
break;
}
}
}
if(count($set) === 0){
return [0,[]];
}
if(isset($set[$desiredSum])){
return [
$desiredSum,
formatResult($numbers,array_keys($set[$desiredSum]))
];
}else{
$keys = array_keys($set);
$nearestSum = end($keys);
$sum = 0;
$rev_numbers = array_reverse($numbers);
$result = [];
foreach($rev_numbers as $number){
$sum += $number;
$result[] = $number;
if($sum > $nearestSum && abs($nearestSum - $desiredSum) > abs($sum - $desiredSum)){
$nearestSum = $sum;
break;
}else if($sum > $nearestSum && abs($nearestSum - $desiredSum) < abs($sum - $desiredSum)){
$result = formatResult($numbers,array_keys($set[$nearestSum]));
break;
}
}
return [
$nearestSum,
$result
];
}
}
function formatResult($numbers,$keys){
$result = [];
foreach($keys as $key) $result[] = $numbers[$key];
return $result;
}
print_r(coinChange([10000,5000,1000,1000],14000));
print_r(coinChange([10000,5000,1000,1000],13000));
print_r(coinChange([100000,100000,100000,100000,100000,100000,50000,50000,50000,50000,10000,10000,500,500,500,1000,1000],250000));
print_r(coinChange([100000,100000,100000,100000,100000,100000,50000,50000,50000,50000,10000,10000,500,500,500,1000,1000],179999));
Demo: https://3v4l.org/hBGeW
Algorithm:
This is similar to coin change problem.
We first sort the numbers.
Now, we iterate from minimum number in the array to the desired sum.
Inside it, we iterate through all elements in the array.
Now, we can make $i(which is a sum) only if we have made sum $i - $current_number. If we have the previous one, then we add $current_number to our collection for sum $i.
Two Scenarios:
If we can make the exact sum, then we return the result as is.
If we can't, then are 2 possibilities:
We would already have nearest sum possible in our $set which would be the last entry. We keep them in a variable.
Now, the nearest sum could also be higher than the desired sum. So, we get the larger sum and check if it's nearer than nearest smallest sum and then compare both and return the result.
Result format:
Let's take the below sample output:
Array
(
[0] => 15000
[1] => Array
(
[0] => 10000
[1] => 5000
)
)
It simply means that the first index is the nearest sum possible and array at 2nd index is all elements it took from $numbers to make that sum.
I'm using googledistancematrix api for calculating distance from login user to all my fields of db. That's my controller code.
$field_list = Field::all();
for ($i=0; $i < count($field_list); $i++)
{
$destination = $field_list[$i]['latitude'] . "," . $field_list[$i]['longitude'];
$details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&mode=driving&sensor=false";
$json = file_get_contents($details);
$details = json_decode($json, TRUE);
if (count($details['destination_addresses']) > 0 )
{
$distance = $details['rows'][0]['elements'][0]['duration']['text'];
$field_list[$i]->distance = $distance;
}
}
By this i'm getting following response:
https://jsoneditoronline.org/?id=a35cedef327244ceb19ed35a2a4c8ddf
But i want to show only those fields whose distance < or = 30 mins.
Thanks
It's a simple matter to convert the time to seconds using strtotime() as follows:
$time=strtotime($details['rows'][0]['elements'][0]['duration']['text']);
if (($time-time()>1800) {
// ignore this element, probably in loop so do a continue
}
This is based on using your example line above which is only for a single element. You would need to wrap it with a loop and perform the calculation for each element.
You can use strtotime
$min = strtotime("30 mins");
$val = strtotime("0 hours 10 mins");
echo $min <= $val? "yes":"no";
(here if you want to test it)
in your case
if (count($details['destination_addresses']) > 0 ){
$min = strtotime("30 mins");
$distance = $details['rows'][0]['elements'][0]['duration']['text'];
if($min <= strtotime($distance)){
//do your logic
}
}
Hi I trying to do the following task:
I used many array functions to accomplish the task with no success. Used inner for loop as well no results cant bring back the weekday element correctly.
Write code to initialise an associative array with keys from 1 to 7 and values representing days of the week (Sunday to Saturday). Write a do while loop that iterates 5 times. In each iteration, it should generate a random number between 1 and 7. Use the random number as the key to access the correct array element to obtain the correct day of the week.
This is my code so far:
$i = 0;
$weekday = array(); // Create Array
$weekday['1'] = 'Sunday';
$weekday['2'] = 'Monday';
$weekday['3'] = 'Tuesday';
$weekday['4'] = 'Wednesday';
$weekday['5'] = 'Thursday';
$weekday['6'] = 'Friday';
$weekday['7'] = 'Saturday';
do {
$num_rang=range(1,7); // range 1 to 7
$result_array = array_combine($num_rang, $weekday); // Trying to combine arrays
shuffle($num_rang); //random number range
for ($x=0; $x< 1; $x++)
{
echo $num_rang[$x].' '; //display the Random number and Weekday corresponding to the number
}
It's simple. See the documentation about PHP rand and PHP do-while
# initialise an associative array with keys from 1 to 7
$weekday = array();
$weekday['1'] = 'Sunday';
$weekday['2'] = 'Monday';
$weekday['3'] = 'Tuesday';
$weekday['4'] = 'Wednesday';
$weekday['5'] = 'Thursday';
$weekday['6'] = 'Friday';
$weekday['7'] = 'Saturday';
$i = 0;
do {
# random number between 1 and 7
$random = rand(1,7);
echo $weekday[$random]."<br/>";
$i++;
} while ($i < 5);
Simple solution is:
$i = 0;
$weekday = array(); // Create Array
$weekday['1'] = 'Sunday';
$weekday['2'] = 'Monday';
$weekday['3'] = 'Tuesday';
$weekday['4'] = 'Wednesday';
$weekday['5'] = 'Thursday';
$weekday['6'] = 'Friday';
$weekday['7'] = 'Saturday';
while ($i < 5) {
echo $weekday[rand(1,7)];
$i++;
}
$weekday = array_combine(
range(1,7),
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
);
$i = 5;
do {
$key = mt_rand(1,7);
echo $key, "\t", $weekday[$key], PHP_EOL;
} while (--$i);
When $i is equal to 0, the condition is evaluated to false and the loop stops. Note that $i is decremented before the condition is evaluated since the -- operator is placed before $i.
I used mt_rand instead of rand since this function is faster. Read more about it.
I've been stumped on this PHP issue for about a day now. Basically, we have an array of hours formatted in 24-hour format, and an arbitrary value ($hour) (also a 24-hour). The problem is, we need to take $hour, and get the next available value in the array, starting with the value that immediately proceeds $hour.
The array might look something like:
$goodHours = array('8,9,10,11,12,19,20,21).
Then the hour value might be:
$hour = 14;
So, we need some way to know that 19 is the next best time. Additionally, we might also need to get the second, third, or fourth (etc) available value.
The issue seems to be that because 14 isn't a value in the array, there is not index to reference that would let us increment to the next value.
To make things simpler, I've taken $goodHours and repeated the values several times just so I don't have to deal with going back to the start (maybe not the best way to do it, but a quick fix).
I have a feeling this is something simple I'm missing, but I would be so appreciative if anyone could shed some light.
Erik
You could use a for loop to iterate over the array, until you find the first that is greater than the one you're searching :
$goodHours = array(8,9,10,11,12,19,20,21);
$hour = 14;
$length = count($goodHours);
for ($i = 0 ; $i < $length ; $i++) {
if ($goodHours[$i] >= $hour) {
echo "$i => {$goodHours[$i]}";
break;
}
}
Would give you :
5 => 19
And, to get the item you were searching for, and some after that one, you could use something like this :
$goodHours = array(8,9,10,11,12,19,20,21);
$hour = 14;
$numToFind = 2;
$firstIndex = -1;
$length = count($goodHours);
for ($i = 0 ; $i < $length ; $i++) {
if ($goodHours[$i] >= $hour) {
$firstIndex = $i;
break;
}
}
if ($firstIndex >= 0) {
$nbDisplayed = 0;
for ($i=$firstIndex ; $i<$length && $nbDisplayed<$numToFind ; $i++, $nbDisplayed++) {
echo "$i => {$goodHours[$i]}<br />";
}
}
Which would give you the following output :
5 => 19
6 => 20
Basically, here, the idea is to :
advance in the array, until you find the first item that is >= to what you are looking for
get out of that first loop, when found
If a matching item was found
loop over the array, until either its end,
or you've found as many items as you were looking for.
You can also use the SPL FilterIterator. Though it's not the fastest solution there is, it has the advantage that you can "prepare" the iterator somewhere/anywhere and then pass it to a function/method that doesn't have to know how the iterator works on the inside, i.e. you could pass a completely different iterator the next time.
class GreaterThanFilterIterator extends FilterIterator {
protected $threshold;
public function __construct($threshold, Iterator $it) {
$this->threshold = $threshold;
parent::__construct($it);
}
public function accept() {
return $this->threshold < parent::current();
}
}
function doSomething($it) {
// no knowledge of the FilterIterator here
foreach($it as $v) {
echo $v, "\n";
}
}
$goodHours = array(8,9,10,11,12,19,20,21);
$it = new GreaterThanFilterIterator(14, new ArrayIterator($goodHours));
doSomething($it);
prints
19
20
21
As $goodHours is already sorted, that's something easy:
$next = 0;
foreach($goodHours as $test)
if($test > $hour && $next = $test)
break;
After that four-liner (that can be written in a smaller number of lines naturally), $next is either 0 if $hour could not be matched in $goodHours or it contains the value that immediately proceeds $hour. That is what you asked for.
This only works when $goodHours is sorted, in case it's not, you can sort it by using the asort() function.
Try this function:
function nextValueGreaterThan($haystack, $needle, $n=1) {
sort($haystack);
foreach ($haystack as $val) {
if ($val >= $needle) {
$n--;
if ($n <= 0) {
return $val;
}
}
}
}
$goodHours = array(8,9,10,11,12,19,20,21);
echo nextValueGreaterThan($goodHours, 14); // 19
echo nextValueGreaterThan($goodHours, 14, 3); // 21
Here's an answer similar to the rest of these, including an optional "offset" parameter, that gets your n'th item past the de-facto first one.
class GoodHours {
private $hours = array(8,9,10,11,12,19,20,21);
public function getGoodHour($hour, $offset = 0) {
$length = count($this->hours);
for ($i = 0 ; $i < $length && $this->hours[$i] < $hour ; $i++)
; // do nothing
return $this->hours[($i + $offset) % $length];
}
}
// some test values
$good = new GoodHours();
$x = $good->getGoodHour(5); // 8
$x = $good->getGoodHour(5,1); // 9
$x = $good->getGoodHour(5,2); // 10
$x = $good->getGoodHour(10); // 10
$x = $good->getGoodHour(10,1); // 11
$x = $good->getGoodHour(10,2); // 12
$x = $good->getGoodHour(21); // 21
$x = $good->getGoodHour(21,1); // 8
$x = $good->getGoodHour(21,2); // 9
$x = $good->getGoodHour(21); // 8
$x = $good->getGoodHour(22,1); // 9
$x = $good->getGoodHour(22,2); // 10