Grouping array based on values - php

I want to group array values
my array looks like below with 30 mins time interval
$arr = ["00:00","00:30","01:00","01:30","02:00","02:30","03:00","04:30","05:00","05:30"];
i want the output like
Array ( [0] => Array ( [0] => 00:00 [1] => 03:00 ),[1]=> Array ( [0] => 04:30 [1] => 05:30 )) ;
Here is my code for achieving the result
$output = array();
$start = $arr[0];
for($i=1; $i<count($arr); $i++) {
if($i == count($arr)-1) {
$interval = array($start,$arr[$i]);
array_push($output,$interval);
break;
}
if((int)($arr[$i]) - (int)($arr[$i-1]) > 1) {
$interval = array($start,$arr[$i-1]);
array_push($output,$interval);
$start = $arr[$i];
}
}
print_r($output);
but i got the result looks like
Array ( [0] => Array ( [0] => 00:00 [1] => 05:30 ) )
Thanks

$times = ["00:00","00:30","01:00","01:30","02:00","02:30","03:00","04:30","05:00","05:30"];
$result = array();
$index = 0;
for ($i=0; $i < count($times); $i++){
if($i == 0){
$result[$index][] = $times[$i];
}elseif($i == count($times)-1){
$result[$index][] = $times[$i];
}else{
if((h2m($times[$i])-h2m($times[$i-1])) > 30){
$result[$index][] = $times[$i-1];
$index++;
$result[$index][] = $times[$i];
}
}
}
var_dump($result);
FUNCTION h2m($hours) {
$t = EXPLODE(":", $hours);
$h = $t[0];
IF (ISSET($t[1])) {
$m = $t[1];
} ELSE {
$m = "00";
}
$mm = ($h * 60) + $t[1];
RETURN $mm;
}

Related

Populate This Multidimensional array with for or foreach

I have this array but I do not know how to populate it dynamically
My code:
for ($j = 1; $j <= $weeks; $j++)
{
for ($i = 1; $i <= 7; $i++)
{
$turn_data = $request->input('turn_'.$i.'_'.$j);
if(isset($turn_data))
{
$turn_data = explode("_", $turn_data);
$turn = Turn::find($turn_data[0]);
if($day < 10)
{
$day = "0".$day;
}
$data = array("apiKey" => "85526dd10b9aa01ae6e56698b848d191",
"turnos" => [["codigo" => $rut,
"nombreTurno" => $turn->name,
"fechaInicio" => $year."-".$month_detail_number."-".$day." 00:00:00",
"fechaTermino" => $year."-".$month_detail_number."-".$day." 23:59:59"]]);
}
$day = $day + 1;
}
}
How can I put the array? The problem is that turnos is an array too, that's my problem
I need an output like this:
Array ( [apiKey] => 85526dd10b9aa01ae6e56698b848d191 [turnos] => Array ( [0] => Array ( [codigo] => 15918421 [nombreTurno] => 6x1 FT/ 08:30 a 16:00 [fechaInicio] => 2019-12-13 00:00:00 [fechaTermino] => 2019-12-13 23:59:59 ) ) )
Thanks!

i need a PHP code to find longest contiguous sequence of characters in the string

I need a PHP code to find longest contiguous sequence of characters in the string. So if b is coming together for maximum number of times your program should echo b and count
Example string:
aaabababbbbbaaaaabbbbbbbbaa
Output must be:
b 8
Using
- preg_match_all to get sequences of repeating characters,
- array_map along with strlen to get the string length of each sequence
- max to get the biggest value in the array.
Consider the following example:
$string = "aaabababbbbbaaaaabbbbbbbbaa";
preg_match_all('#(\w)\1+#',$string,$matches);
print_r($matches);
Will output
Array
(
[0] => Array
(
[0] => aaa
[1] => bbbbb
[2] => aaaaa
[3] => bbbbbbbb
[4] => aa
)
[1] => Array
(
[0] => a
[1] => b
[2] => a
[3] => b
[4] => a
)
)
Next we get the sizes for each string of repeating characters
$sizes = array_map('strlen', $matches[0]);
print_r($sizes);
Will output
Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 8
[4] => 2
)
Now let's get the biggest value of the $sizes array
print max($sizes);
Will give us
8
We need the key for the max value to pick up the letter
$maxKey = array_keys($sizes, max($sizes));
print $matches[1][$maxKey[0]];
Will output
b
Since you're looking for continuous sequences:
$string = 'aaabababbbbbaaaaabbbbbbbbaa';
$count = strlen($string);
if ($count > 0)
{
$mostFrequentChar = $curChar = $string[0];
$maxFreq = $curFreq = 1;
for ($i = 1; $i < $count; $i++)
{
if ($string[$i] == $curChar)
{
$curFreq++;
if ($curFreq > $maxFreq)
{
$mostFrequentChar = $curChar;
$maxFreq = $curFreq;
}
}
else
{
$curChar = $string[$i];
$curFreq = 1;
}
}
}
echo $mostFrequentChar . ' ' . $maxFreq;
$string = 'aaabababbbbbaaaaabbbbbbbbaa';
$occurrence = [];
$count = strlen($string);
for ($x = 0; $x < $count; $x++) {
if(isset($ocurrence[$string[$x]])) {
$ocurrence[$string[$x]]++;
} else {
$ocurrence[$string[$x]] = 0;
}
}
var_dump($occurrence);
This should do the trick.
<?php
$x = "aaaaaaabbbbbbbaaaacccccccaaaaaaaaaaaaaaaaabbbbbbaaaaadddddddddddddddddddddddddddddddddd";
$count = strlen($x);
$y =array();
$n =0;
$d =1;
$first = $x{1};
for($j = $d;$j < $count;$j++)
{
if($x{$j} == $first )
{
$y[$j] = $x{$j};
$first = $x{$j};
}
elseif($x{$j} != $first )
{
$y[$j] = ",".$x{$j};
$first = $x{$j};
}
}
$xy = implode("",$y);
$xy1 = explode(",",$xy);
$c_count = count($xy1);
$dg = 1;
for($g = 0;$g < $c_count;$g++)
{
$cnt = strlen($xy1[$g]);
$cntm = $xy1[$g];
$replace = $cntm{1};
if($cnt > $dg)
{
$ab = str_replace($cntm,$replace,$cntm);
$dg = $cnt.".".$ab ;
}
}
echo $dg;
?>

Generate combination array PHP [duplicate]

This question already has answers here:
PHP array combinations
(8 answers)
Closed 7 years ago.
Hi Guys i have an array like this
$array1 = array('a','b','c','d)
and i want to combine with output like this
'a,b,c'
'a,b,d'
'a,c,d'
'b,c,d'
The problem is to create a function with a variable number and not multiple variable, can anybody help me?
Make a try
[akshay#localhost tmp]$ cat permutation_comb.php
<?php
function _perm($comb,$arr)
{
$arr_len = count($arr);
$comb = intval($comb);
if ($comb > $arr_len)
{
$p = 0;
}
elseif ($arr_len == $comb)
{
$p = 1;
}
else {
if ($comb >= $arr_len - $comb)
{
$l = $comb+1;
for ($i = $l+1 ; $i <= $arr_len ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $arr_len-$comb ; $i++)
$m *= $i;
}
else {
$l = ($arr_len-$comb) + 1;
for ($i = $l+1 ; $i <= $arr_len ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $comb ; $i++)
$m *= $i;
}
}
if(!isset($p)){ $p = $l/$m ; }
$out = array_fill(0, $p, array_fill(0, $comb, '') );
$t = array();
for ($i = 0 ; $i < $comb ; $i++)
$t[$i] = $i;
$out[0] = $t;
for ($i = 1 ; $i < $p ; $i++)
{
if ($t[$comb-1] != count($arr)-1)
{
$t[$comb-1]++;
}
else {
$xx = -1;
for ($j = $comb-2 ; $j >= 0 ; $j--)
if ($t[$j]+1 != $t[$j+1])
{
$xx = $j;
break;
}
if ($xx == -1)
break;
$t[$xx]++;
for ($j = $xx+1 ; $j < $comb ; $j++)
$t[$j] = $t[$xx]+$j-$xx;
}
$out[$i] = $t;
}
for ($i = 0 ; $i < $p ; $i++)
for ($j = 0 ; $j < $comb ; $j++)
$out[$i][$j] = $arr[$out[$i][$j]];
return $out;
}
$Input = array('a','b','c','d');
$output = array_map(function($a){ return implode(",",$a); },_perm(3, $Input));
// Input
print_r($Input);
// Combination output
print_r($output);
?>
Output
[akshay#localhost tmp]$ php permutation_comb.php
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Array
(
[0] => a,b,c
[1] => a,b,d
[2] => a,c,d
[3] => b,c,d
)
To get all possible combination of chars modify calling part of function like below
$output = array();
for($i=1; $i<=count($Input); $i++)
{
$output = array_merge($output, array_map(function($a){ return implode(",",$a); },_perm($i, $Input)) ) ;
}
Which results
[akshay#localhost tmp]$ php permutation_comb.php
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => a,b
[5] => a,c
[6] => a,d
[7] => b,c
[8] => b,d
[9] => c,d
[10] => a,b,c
[11] => a,b,d
[12] => a,c,d
[13] => b,c,d
[14] => a,b,c,d
)

Adding missing dates for 1 week back from now

I am trying to add the missing dates to this array for 1 week back from now.
This is an example array I have;
Array
(
[0] => Array
(
[date] => 2013-11-25
[members] => 2
)
[1] => Array
(
[date] => 2013-11-27
[members] => 1
)
)
This could have any dates in. I tried things like this, but I can see logically It doesn't work, but I cannot figure out a way.
$date_range = array();
$temp = array();
for ($i=0; $i<7; $i++)
{
$date = date("Y-m-d", strtotime($i." days ago"));
foreach($new_members as $members) {
if(!in_array($date, $members)) {
$temp['date'] = $date;
$temp['members'] = 0;
$new_members[] = array_merge($temp);
}
}
}
Solution provided by Dainis doesn't work for me, it's messing up the members and date.
Here is my solution:
<?php
$new_members = array ( array("date"=>"2013-11-25", "members" => 2), array("date"=>"2013-11-27", "members" => 2));
for ($i=0; $i<7; $i++)
{
$date = date("Y-m-d", strtotime($i." days ago"));
$found = false;
foreach($new_members as $members) {
if(array_search($date, $members) !== false) {
$found = true;
}
}
if(!$found) {
$new_members[] = array ("date" => $date, "members" => 0);
}
}
foreach($new_members as $nm) {
var_dump($nm);
}
?>
Instead of:
$temp['date'] = $date;
$temp['members'] = 0;
$new_members[] = array_merge($temp);
just do it like this:
$new_members[$i]['date'] = $date;
if ( !$new_members[$i]['members'] ) $new_members[$i]['members'] = 0;
After that you can sort it after ['date'] value and get a nice looking array.

PHP Multidimensional Array problem

iam trying to build a multidimensional array.
public function saveRateTemplateData($RateTemplateInfo)
{
$RateTemplateID = $RateTemplateInfo['id'];
$Zones = $RateTemplateInfo['premium_zones'];
//$ZoneZipCodeIDs[] = array();
for ($n = 1; $n <= $RateTemplateInfo['premium_zones']; $n++) {
$ZoneNum = 'zone' . $n;
$ZipCodeArray = explode(",",$_POST[$ZoneNum]);
$ZipCodeIDs=array();
foreach ($ZipCodeArray as $v) {
$v = intval(trim($v));
if (strlen($v) == 5) {
array_push($ZipCodeIDs, $this->addZipCode($v));
} else {
echo "it isnt 5";
}
}
}
}
so what iam trying to do is make an array of an array. so this is how its supposed to look
Array
(
[1] => Array
(
[0] => 34
[1] => 31
[2] => 23
)
[2] => Array
(
[0] => 18
[1] => 4
[2] => 35
[3] => 1
)
)
i have tried numerous ways it doesnt work
basically i want it in this format VarName[ZoneNumbers][ZipCodeID]
so i can loop through it later on. so i can print like this $VarName[$n] then a array of all zipcodeID will print for Zone Number 1 in this case it will print 34,31,23
public function saveRateTemplateData($RateTemplateInfo)
{
$RateTemplateID = $RateTemplateInfo['id'];
$zones = array(); // you weren't using this so I'll use it to hold the data
for ($n = 1; $n <= $RateTemplateInfo['premium_zones']; $n++) {
$ZoneNum = 'zone' . $n;
// create an array under the zone number for holding the IDs
$zones[$n] = array();
$ZipCodeArray = explode(",",$_POST[$ZoneNum]);
foreach ($ZipCodeArray as $v) {
$v = (int) trim($v);
if (strlen($v) == 5) {
$zones[$n][] = $this->addZipCode($v);
} else {
// use exceptions for exceptional circumstances
throw new RuntimeException(sprintf('Invalid zone ID "%s"', $v));
}
}
}
return $zones;
}

Categories