Grouping dates in array [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array with many dates. These dates are passive. If the free time in between is more than 8 days, i need to get the opposite of these dates. I have to process these opposite dates as the number of day, start date and end date.
Example Array
Array
(
[0] => 17-05-2020
[1] => 18-05-2020
[2] => 19-05-2020
[3] => 20-05-2020
[4] => 28-05-2020
[5] => 29-05-2020
[6] => 30-05-2020
[7] => 02-06-2020
[8] => 03-06-2020
[9] => 10-07-2020
[10] => 10-07-2020
[11] => 11-07-2020
[12] => 12-07-2020
[13] => 13-07-2020
[14] => 20-07-2020
[15] => 21-07-2020
[16] => 25-07-2020
[17] => 26-07-2020
[18] => 27-07-2020
)
The result I want
Array
(
[0] => Array
(
[0] => 30-05-2020
[1] => 02-06-2020
[2] => 3
)
[1] => Array
(
[0] => 13-07-2020
[1] => 20-07-2020
[2] => 7
)
[2] => Array
(
[0] => 21-07-2020
[1] => 25-07-2020
[2] => 4
)
)

What i understood is you want to get difference between dates and if difference is >= 8 then you want those dates in separate array with days counts as well
Do like below:
$finalArray = [];
foreach($array as $key=>$arr){
if(isset($array[$key+1])){
$diff = round((strtotime($array[$key+1]) - strtotime($arr)) / (60 * 60 * 24));
if(($diff < 8) and ($diff > 1)){
$finalArray[] = array(
$arr,
$array[$key+1],
$diff
);
}
}
}
print_r($finalArray);
Output : https://3v4l.org/QBYYR

Related

Single to multiple Array in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In my foreach loop, I'm fetching data from csv file, after rendering it in the array, the data is rendered collectively, but I want it to show column wise as in csv file or I have shown in the below second code example:
Array(
[0] => op1
[1] => op2
[2] => ans 1
[3] => op4
[4] => op1
[5] => op2
[6] => ans 2
[7] => op4
[8] => op1
[9] => op2
[10] => ans 3
[11] => op4
[12] => op1
[13] => op2
[14] => ans 4
[15] => op4
[16] => op1
[17] => op2
[18] => ans 5
[19] => op4
[20] => op1
)
I want to this
Array
(
option[0]=>
Array(
[0] => op1
[1] => op2
[2] => ans 1
[3] => op4
)
option[1]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
option[2]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
option[3]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
)
you can use array_chuck() function like this:
print_r(array_chunk($input_array, 4));
In your existing foreach loop use Modulus for divide array in groups.
$final_arr= array();
$new_key = 0;
foreach($csv_data as $key => $value){
$new_key = $new_key;
if($key % 4 == 0){
$new_key += 1;
}
$final_arr[$new_key][]= $value;
}
Here I assume you got your csv data in $csv_data this variable.
Hope it helps to you.
From str_getcsv's manual page, first two comments:
That does not produce the desired results, if fields contain
linebreaks.
Handy one liner to parse a CSV file into an array:
$csv = array_map('str_getcsv', file('data.csv'));
This will create an array of associative arrays with the first row
column headers as the keys.
$csv = array_map('str_getcsv', file($file));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv); // remove column header

PHP multi-dimensional array, merge duplicate keys into new arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following array:
Array (
[ids] => Array (
[0] => e348ae92
[1] => 193ba701
[2] => 58695854
)
[name] => Array (
[0] => Customers
[1] => Suppliers
[2] => Users
)
[subs] => Array (
[0] => 614
[1] => 65
[2] => 99
)
)
I want to take each array key and turn it into a it's own array e.g.
array(
[0] = array(
[0] => e348ae92
[1] => custommers
[2] => 614
)
[1] = array(
[0] => 193ba701
[1] => Suppliers
[2] => 65
)
[2] = array (
[0] => 58695854
[1] => Users
[2] => 99
)
)
I have looked at array_merge, array_combine and a few other things but I have been unsuccessful so far, any pointers in the right direction would be appreciated.
You can use array_map
$f = array_map(null, $a['ids'], $a['name'], $a['subs']);
print_r($f);
Working example:https://3v4l.org/dDG0Y-

num. Array, Sum same values php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this array (much more longer - only part)
[366] => Array
(
[0] => 8
[1] => 7
[2] => 8
[3] => 9
[4] => 8
)
[367] => Array
(
[0] => 8
[1] => 9
[2] => 9
[3] => 10
[4] => 10
[5] => 9
[6] => 8
[7] => 9
[8] => 10
[9] => 10
[10] => 11
[11] => 10
[12] => 11
[13] => 11
[14] => 10
[15] => 9
[16] => 10
[17] => 8
[18] => 10
)
The keys must be hold (there are an id). I want to sum the values foreach (in this example the values are generations). So Generation 8 is count 3 times for 366.
Expect is for id 366: 7 - 1x, 8 - 3x, 9 - 1x or
[366] => Array
(
[7] => 1
[8] => 3
[9] => 1
)
[367] => Array
(
[8] => 3
[9] => 5
[10] => 8
[11] => 3
)
Every suggestion is welcomed and appreciated!
I´m always stopped with the numeric keys or the foreach ... one of code i´ve tried:
$outdams = array();
foreach ($dams as $key => $value) {
foreach ($value as $key2 => $value2) {
$index = $value2;
if (array_key_exists($index, $outdams)) {
$outdams[$index]++;
} else {
$outdams[$index] = 1;
}
}
}
check this code
<?php
$data = array(366=>array(8,7,8,8,9),
367=>array(8,9,13,14,17,14));
echo "<pre>"; print_r($data);
$response = array();
foreach ($data as $outerkey => $outerData){
foreach ($outerData as $innerkey => $innerData){
//echo "<pre>"; print_r($innerData);
if(array_key_exists($innerData, $response[$outerkey])){
$response[$outerkey][$innerData] ++;
}else{
$response[$outerkey][$innerData] = 1;
}
}
}
echo "<pre>"; print_r($response);
?>

Combine multi array to one array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a PHP array like the following one and I would like to combine a multi array to one array:
Array
(
[0] => ADB_DW2017
[1] => LM9
[2] => MS_OF2013
)
Array
(
[0] => NK
[1] => PV
[2] => NK
)
Array
(
[0] => 15
[1] => 25
[2] => 10
)
Array
(
[0] => 250
[1] => 111
[2] => 150
)
Array
(
[0] => 450
[1] => 123
[2] => 250
)
Array
(
[0] => 0
[1] => Mien thue
[2] => 5
)
Array
(
[0] => 200
[1] => 12
[2] => 100
)
Array
(
[0] => 6.750
[1] => 3.075
[2] => 2.500
)
I want to combine like this, with the highest performance :
Array
(
[ADB_DW2017]=>array([suppiler]=>NK
[min_pro]=>15
[max_pro]=>250
[avg_pro]=>450
[tax]=>0
[com]=>200
[sum]=>6.750
)
)
My question is : How to combine a multi array to one array in PHP with the highest performance ?
I assume array names are $first_array,$second_array,...... (so change varabile names accordingly).
Do like below:-
$final_array = array();
foreach ($first_array as $key=> $arr){
$final_array[$arr] =array(
'suppiler'=>(isset($second_array[$key]))? $second_array[$key]: '',
'min_pro'=>(isset($third_array[$key]))? $third_array[$key]: 0,
'max_pro'=>(isset($fourth_array[$key]))? $fourth_array[$key]: 0,
'avg_pro'=>(isset($fifth_array[$key]))? $fifth_array[$key]: 0,
'tax'=>(isset($sixth_array[$key]))? $sixth_array[$key]: '',
'com'=>(isset($seventh_array[$key]))? $seventh_array[$key]: 0,
'sum'=>(isset($eigth_array[$key]))? $eigth_array[$key]: 0
);
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/829938

Split multidimensional arrays into multiple single arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to split the below array into 3 single arrays. However, the size of the array can be any number. The below array size is 3.
Array (
[0] => Array
(
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
[1] => Array
(
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
[2] => Array
(
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
)
Expected output:
Array1 (
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
Array2 (
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
Array3 (
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
The name of the individual array should be followed by a number that increments for each array.
You can break a 3d array into 3 separate arrays using list().
list($array1, $array2, $array3) = $mainArray;
Here's an example: https://3v4l.org/hQ1Te
Now if you don't know how many arrays are going to be in the input array you can do something like this...
for($i=0; $i<count($mainArray); $i++){
$variableName = "array$i";
$$variableName = $mainArray[$i];
}
var_dump($array1, $array2, $array3, $array4, ...);
So are you looking for something like this?
for ($i = 0; $i < count($inputArray); $i++) {
echo "Array ". $i;
echo "<pre>";
print_r($inputArray[$i]);
echo "</pre>";
}
Edit: ok now I get it, you want:
for ($i = 0; $i < count($inputArray); $i++) {
$name = "array" . ($i + 1);
$$name = $inputArray[$i];
}

Categories