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!
Related
$data1 = array();
$final_ttt = "15";
$items = [];
for ($j = 1; $j <= $final_ttt; ++$j)
{
$TeamNo = "t$j";
$items[] = $TeamNo;
}
print_r($items);
In while loop im getting below values
$day = 1;
$i = 0;
while($row = $qry->fetch())
{
$name = $row['name']; //a,b,c,d
$loopvalue = $row['loopvalue']; // getting 2,3,8,3,4
$data1[]=array("name" => $name,"loopvalue" => $loopvalue);
if( $i % $final_ttt == 0 )
{
$fday = "Day ".$day;
$day++;
}
}
output
name value Team day
a 2 t1,t12 1
b 3 t3,t4,t5 1
c 8 t5,t6,t7,t8,t9,t10,t11,t12 1
d 3 t13,t14,t15 1
e 4 t1,t2,t3,t4 2
How to allocate the teams to based on while loop value. if i get value is 2 => then i need to allocate first 2 teams t1,t2 like this. kinldy help me.
Thanks you in advance
--- We are editing the question as the solution provided does not work in certain test cases. Like the one shown in the attached image.
Here is the updated code for above image output
$lastIndex = 0;
$day = 1;
while($row = $qry->fetch())
{
$name = $row['name']; //a,b,c,d
$loopvalue = $row['loopvalue']; // getting 2,3,8,3,4
if($i==0)
{
$fday = "Day ".$day;//Day 1 for first Itration
$day = 2;
}
$team = implode(',',array_slice($items, $lastIndex, $loopvalue));
$lastIndex = $lastIndex + $loopvalue;
if($lastIndex > count($items))
{
$lastIndex = $lastIndex - count($items);
$team .= ','.implode(',',array_slice($items, 0, $lastIndex));
$fday = "Day ".$day;
$day++;
}
$data1[]=array("name" => $name,"loopvalue" => $loopvalue, "day" => $fday, 'team' => $team);
$row ++;
}
print_r($data1);
You can try something like below
$lastIndex = 0;
while($row = $qry->fetch())
{
$name = $row['name']; //a,b,c,d
$loopvalue = $row['loopvalue']; // getting 2,3,8,3,4
$team = implode(',',array_slice($items, $lastIndex, $loopvalue));
$lastIndex = $lastIndex + $loopvalue;
if($lastIndex > count($items)) {
$lastIndex = $lastIndex - count($items);
$team .= ','.implode(',',array_slice($items, 0, $lastIndex));
}
$data1[]=array("name" => $name,"loopvalue" => $loopvalue, 'team' => $team);
$row ++;
}
print_r($data1);
Will result like
Array ( [0] => Array ( [name] => a [loopvalue] => 2 [team] => t1,t2 ) [1] => Array ( [name] => b [loopvalue] => 3 [team] => t3,t4,t5 ) [2] => Array ( [name] => c [loopvalue] => 8 [team] => t6,t7,t8,t9,t10,t11,t12,t13 ) [3] => Array ( [name] => d [loopvalue] => 3 [team] => t14,t15,t1 ) [4] => Array ( [name] => e [loopvalue] => 4 [team] => t2,t3,t4,t5 ) )
Let me know if this solve your problem.
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;
}
Hi I've got the following array:
$days = array(
1=>"Sunday",
2=>"Monday",
3=>"Tuesday",
4=>"Wednesday",
5=>"Thursday",
6=>"Friday",
7=>"Saturday"
);
Now I want to make a loop that automatically removes all objects before number 4.
I tried this:
$startIndex = 4;
for($i = 1; $days < $startIndex; $i++)
{
unset($days[$i]);
}
But it does not work.
A shorter solution may be given using array_slice():
$days = array(
1=>"Sunday",
2=>"Monday",
3=>"Tuesday",
4=>"Wednesday",
5=>"Thursday",
6=>"Friday",
7=>"Saturday"
);
$startIndex = 4;
$days = array_slice($days, $startIndex-1, NULL, TRUE);
print_r($days);
returns
Array
(
[4] => Wednesday
[5] => Thursday
[6] => Friday
[7] => Saturday
)
Change $days to $i as $i is your index value.
$days = array(
1=>"Sunday",
2=>"Monday",
3=>"Tuesday",
4=>"Wednesday",
5=>"Thursday",
6=>"Friday",
7=>"Saturday"
);
$startIndex = 4;
for($i = 1; $i < $startIndex; $i++)
{
unset($days[$i]);
}
print_r($days);
Your array
$days = array
(
1=>"Sunday",
2=>"Monday",
3=>"Tuesday",
4=>"Wednesday",
5=>"Thursday",
6=>"Friday",
7=>"Saturday"
);
Loop to remove all element before a specified index.
# Number to stop the unset.
$split_number =4;
# Loop through array
for($a=0;$a<sizeof($days);$a++)
{
if($a < $split_number)
# Unset element if condition is true
unset($days[$a]);
}
print_r($days);
Result
Array
(
[4] => Wednesday
[5] => Thursday
[6] => Friday
[7] => Saturday
)
If you wish the indexes to start from 0 again, you can use the array_values
I have two arrays like so (however there can be more or less than 2 (any amount)):
[0] => Array
(
[assessedUsers] => Array
(
[0] => Array
(
[scores] => Array
(
[0] => 10
[1] => 10
[2] => 10
[3] => 10
)
)
[1] => Array
(
[scores] => Array
(
[0] => 9
[1] => 10
[2] => 0
[3] => 9
)
)
)
)
Where the length of the scores array is always the same in both arrays.
I would like to take each element from each array, one by one, and average them, then append them into a new array.
For example, the output of my desired function would look like this:
[1] => Array
(
[scores] => Array
(
[0] => 9.5
[1] => 10
[2] => 5
[3] => 9.5
)
)
Is there a function that can do this, or do I need a couple nested for() loops? If I need to use forl loops how would I go about doing it? I'm a little confused on the logic behind it.
Currently what I have is:
for ($i = 0; $i < sizeof($data["assessedUsers"]); $i++) {
for ($j = 0; $j < sizeof($data["assessedUsers"][$i]["scores"]); $j++) {
}
}
and I'm a little confused as to what to where to go next. Thanks in advance!
$mean = array_map( function($a, $b) { return ($a + $b) / 2; },
$data['assessedUsers'][0]['scores'],
$data['assessedUsers'][1]['scores']
);
var_dump($mean);
And append $mean anywhere you want. Or do you have more than 2 arrays? You did not state it in your question.
ps: for any number of subarrays
$arr = array(
array('scores' => array(10,10,10,10)),
array('scores' => array(9,10,0,9)),
array('scores' => array(1,2,3,4))
);
// remove arrays from the key
$tmp = call_user_func_array( function() { return func_get_args(); },
array_map( function($a) { return $a['scores']; }, $arr)
);
// add arrays by each element
$mean = array_map( function($val, $ind) use($tmp) {
$sum = 0;
foreach($tmp as $i => $t)
$sum += $t[$ind];
return $sum / ($i + 1);
}, $tmp[0], array_keys($tmp[0]));
var_dump($mean);
Probably two loops:
$newarray();
foreach($main_array as $user) {
foreach($user['assessedUser'][0]['scores'] as $score_key => $user0_value) {
$user1_value = $user['assessedUser'][1]['scores'][$score_key];
$average = ($user1_value + $user0_value) / 2;
... stuff into new array
}
}
I have solution for you, hope this help :)
$scores = array();
for ($i = 0; $i < sizeof($data["assessedUsers"]); $i++) {
for ($j = 0; $j < sizeof($data["assessedUsers"][$i]["scores"]); $j++) {
if(isset($scores[$j])){
$scores[$j] = ($scores[$j] + $data["assessedUsers"][$i]["scores"][$j]) / ($i +1);
}else{
$scores[] = $data["assessedUsers"][$i]["scores"][$j];
}
}
}
$scores[] = $scores;
view Example :)
http://codepad.org/upPjMEym
I try to write a script and a problem. Can you let me know if you know how i can do this or ask someone if they know how can this be possibe.
Max numbers which can be selected 1 to 20 numbers. it can loop and select any number between 1-20
ignore these numbers e.g. 1,2,4,6,9,12 this will be array which can change.
each array line can have upto 4 numbers
Each array line needs to be unique
5.I need to have around 10 arrays unique
Max 2 numbers can match previous numbers see below.
How can i go about doing this. Any help would be great.
e.g.
Array(
[0] => Array
(
[0] => 3
[1] => 16
[2] => 22
[3] => 24
)
[1] => Array
(
[0] => 3
[1] => 16
[2] => 7
[3] => 13
)
[2] => Array
(
[0] => 20
[1] => 17
[2] => 10
[3] => 18
)
)
This not allow as some array match each other
Array(
[0] => Array
(
[0] => 3
[1] => 16
[2] => 22
[3] => 24
)
[1] => Array - cant have this as 3 of the numbers matchs the previous array.only two numbers can match.
(
[0] => 3
[1] => 16
[2] => 22
[3] => 13
)
[2] => Array
(
[0] => 20
[1] => 17
[2] => 10
[3] => 18
)
)
Thank you.
This seems to satisfy your conditions: http://codepad.viper-7.com/WHkQeD
<?php
$num_arrays = 10; $num_elements = 4;
$min = 1; $max = 20;
$exclude_numbers = array( 1, 4, 6); // Add numbers here to exclude
$answer = array();
for( $i = 0; $i < $num_arrays; $i++)
{
$answer[$i] = array();
for( $j = 0; $j < $num_elements; $j++)
{
do
{
$current = rand( $min, $max);
// If the previous array exists and there are more than two common elements when we add the $current element, continue
if( isset( $answer[$i-1]) && count( array_intersect( $answer[$i-1], array_merge( $answer[$i], array( $current))) > 2)
{
continue;
}
} while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i]));
$answer[$i][$j] = $current;
}
}
var_dump( $answer);
Edit: Here is a complete solution that satisfies all of your criteria.
Demo
<?php
$num_arrays = 10; $num_elements = 4;
$min = 1; $max = 20;
$exclude_numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
$answer = array();
for( $i = 0; $i < $num_arrays; $i++)
{
$answer[$i] = array();
for( $j = 0; $j < $num_elements; $j++)
{
do
{
// Get a random element
$current = rand( $min, $max);
$new_array = array_merge( $answer[$i], array( $current));
// If the previous array has more than two common elements (because of the added $current), get a new $current
if( isset( $answer[$i-1]) && count( array_intersect( $answer[$i-1], $new_array)) > 2)
{
$answer[$i] = array_diff( $new_array, $answer[$i-1]);
$j = count( $answer[$i]) - 1;
continue;
}
} while( in_array( $current, $exclude_numbers) || in_array( $current, $answer[$i]));
$answer[$i][$j] = $current;
// If the array is complete, we need to check for unique arrays
if( count( $answer[$i]) == $num_elements)
{
$k = $i - 1;
while( $k >= 0)
{
if( count( array_diff( $answer[$k], $answer[$i])) == 0)
{
// This array is the same as a previous one, start over
$answer[$i] = array();
$j = -1;
break;
}
$k--;
}
// Optionally sort each array
sort( $answer[$i]);
}
}
}
var_dump( $answer);
somthing like:
function generate_list($max, $forbidden)
{
$list = array();
while(count($list) < 4)
{
$new = rand(1, $max);
if(in_array($new, $forbidden))
{
continue;
}
$list[] = $new;
$forbidden[] = $new;
}
return $list;
}
function count_max_same($new_list, $old_lists)
{
$max_same = 0;
foreach($old_lists as $current_list)
{
$max_same = max($max_same, count(array_intersect($new_list, $old_lists)));
}
return $max_same;
}
function generate_unique_lists($count_of_lists, $max, $forbidden, $max_same = 2, $max_tries = 1000)
{
$lists = array();
while($max_tries-- AND count($lists) < $count_of_lists)
{
$new_list = generate_list($max, $forbidden);
if(count_max_same($new_list, $lists) <= $max_same)
{
$lists[] = $new_list;
}
}
return $lists;
}