I have array A and B with the structure:
A:
Array
(
[0] => 2013-08-01
[1] => 2013-08-02
[2] => 2013-08-03
[3] => 2013-08-04
)
and B:
Array
(
[0] => Array
(
[0] => 2013-08-01
[1] => 2
[2] => 0
[3] => 0
)
[1] => Array
(
[0] => 2013-08-02
[1] => 0
[2] => 4
[3] => 0
)
[2] => Array
(
[0] => 2013-08-04
[1] => 0
[2] => 1
[3] => 0
)
)
The question is I want to combine twi ARRAY (A & B) to be like this:
Array
(
[0] => Array
(
[0] => 2013-08-01
[1] => 2
[2] => 0
[3] => 0
)
[1] => Array
(
[0] => 2013-08-02
[1] => 0
[2] => 4
[3] => 0
)
[2] => Array
(
[0] => 2013-08-03
[1] => 0
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => 2013-08-04
[1] => 0
[2] => 1
[3] => 0
)
)
How to merge the both of array?
function combine($a, $b) {
foreach($a as $item_a) {
foreach($b as $item_b) {
if(in_array($item_a, $item_b)) {
continue 2;
}
}
$b[] = array(
$item_a, 0, 0, 0
);
}
usort($b, function($a, $b){return $a[0] > $b[0];});
return $b;
}
<?
$a = Array
(
"2013-08-01",
"2013-08-02",
"2013-08-03",
"2013-08-04"
);
$b = Array
(
Array (
"2013-08-01",
2,
0,
0
),
Array (
"2013-08-02",
0,
4,
0
),
Array (
"2013-08-04",
0,
1,
0
)
);
foreach ($a as $a_item) {
$found = FALSE;
foreach ($b as $b_item) {
if($b_item[0]==$a_item) {
$found = TRUE;
}
}
if(!$found) {
$b[] = Array (
$a_item,
0,
0,
0
);
}
}
print_r ($b);
?>
This should work if you don't have a huge amount of data in the arrays. If you have a large amount of data you should probably do some redesign to make it faster.
Assuming that the core dates are in array B, my approach would be to loop through array A and add to array B if a date is found that is not already in array B.
foreach ($arrA as $datecheck) {
$blnAdd = 1;
foreach ($arrB as $compare) {
if ($compare[0] == $datecheck) {
// If found, don't add!
$blnAdd = 0;
}
}
if ($blnAdd == 1) {
$arrB[] = array($datecheck, 0, 0, 0);
}
}
// When done, sort array B
// Create helper array for multisort
$arrHelper = array();
foreach ($arrB as $data) {
$arrHelper = $data[0];
}
// Then, sort array B using values from helper array
array_multisort($arrHelper, $arrB);
Then, $arrB should contain the values you need.
Related
I have two associative arrays like
Array
(
[0] => Array
(
[0] => 2022-01-19
[1] => 6
)
[1] => Array
(
[0] => 2022-01-20
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-22
[1] => 2
)
)
and
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-23
[1] => 2
)
)
I need to merge them with the date and want a result array-like below
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 0
[2] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 0
[2] => 1
)
[2] => Array
(
[0] => 2022-01-19
[1] => 6
[2] => 0
)
[3] => Array
(
[0] => 2022-01-20
[1] => 1
[2] => 0
)
[4] => Array
(
[0] => 2022-01-21
[1] => 1
[2] => 1
)
[5] => Array
(
[0] => 2022-01-22
[1] => 2
[2] => 0
)
[6] => Array
(
[0] => 2022-01-23
[1] => 0
[2] => 2
)
)
I tried with the below code but not any success.
$final_array = [];
foreach($openTicket as $val){
$closeTicketNo = 0;
foreach($closeTicket as $value){
if($val[0] == $value[0]){
$closeTicketNo = $value[1];
}
}
$final_array[] = [$val[0],$val[1],$closeTicketNo];
}
I get all the elements from the $openTicket but not get all the elements from a $closeTicket to my result array $final_array
This code first finds all of the unique dates (using array_unique) from the first values in each array (array_column fetches the values and array_merge puts them into 1 array).
Then it indexes each array by the dates (using array_column again).
Finally looping through the unique dates and adding a new element to the output with the values (using ?? 0 so that if no value is present the array is still filled properly)...
$dates = array_unique(array_merge(array_column($openTicket, 0), array_column($closedTicket, 0)));
$open = array_column($openTicket, 1, 0);
$closed = array_column($closedTicket, 1, 0);
$finalArray = [];
foreach ($dates as $date) {
$finalArray[] = [$date, $open[$date] ?? 0, $closed[$date] ?? 0];
}
You can try like this
$array1 = [
['2022-01-19',6],
['2022-01-20',1],
['2022-01-21',0]
];
$array2 = [
['2022-01-17',6],
['2022-01-20',2],
['2022-01-21',1]
];
function mergeMultiple($array1,$array2){
foreach($array1 as $item1){
$mergedArray[$item1[0]] = $item1;
}
foreach($array2 as $item2){
if(isset($mergedArray[$item2[0]])){
array_push($mergedArray[$item2[0]],$item2[1]);
$mergedArray[$item2[0]] = $mergedArray[$item2[0]];
}else{
$mergedArray[$item2[0]] = $item2;
}
}
return array_values($mergedArray);
}
$mergedArray = mergeMultiple($array1,$array2);
ksort($mergedArray);
print_r($mergedArray);
I need to count consecutive repeated elements in the array, but if they are non consecutive I want to count them also, to have them in the same sequence as they are.
For example I have flowing array:
$array = Array('a', 'b', 'a', 'a', 'a', 'c', 'c', 'b', 'b', 'a', 'a', 'a', 'a');
if I use array_count_values function
example:
print_r(array_count_values($array));
Output will be:
Array ( [a] => 8 [b] => 3 [c] => 2 )
But I need to get output like this:
element counted value
a 1
b 1
a 3
c 2
b 2
a 4
You can iterate over the array, counting values while they are the same, and pushing a value to the result array when they are different:
$result = [];
$count = 0;
$current = $array[0];
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] == $current) {
$count++;
}
else {
$result[] = array($current, $count);
$count = 1;
}
$current = $array[$i];
}
$result[] = array($current, $count);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 1
)
[2] => Array
(
[0] => a
[1] => 3
)
[3] => Array
(
[0] => c
[1] => 2
)
[4] => Array
(
[0] => b
[1] => 2
)
[5] => Array
(
[0] => a
[1] => 4
)
)
Demo on 3v4l.org
Starting with an array with the first item from the source (and a count of 1), this then starts from the second item and checks if it matches the one in the current output. Adding 1 if it matches or adding a new entry if it doesn't...
$array = Array ('a','b','a','a','a','c','c','b','b','a','a','a','a');
$output = [ ["element" => $array[0], "count" => 1 ] ];
$outputPoint = 0;
for ( $i = 1, $count = count($array); $i < $count; $i++ ) {
if ( $array[$i] == $output[$outputPoint]["element"] ) {
$output[$outputPoint]["count"]++;
}
else {
$output[++$outputPoint] = ["element" => $array[$i], "count" => 1 ];
}
}
print_r($output);
Which outputs...
Array
(
[0] => Array
(
[element] => a
[count] => 1
)
[1] => Array
(
[element] => b
[count] => 1
)
[2] => Array
(
[element] => a
[count] => 3
)
[3] => Array
(
[element] => c
[count] => 2
)
[4] => Array
(
[element] => b
[count] => 2
)
[5] => Array
(
[element] => a
[count] => 4
)
)
This loops through the array and increments the number of elements in $count.
$array = ['a','b','a','a','a','c','c','b','b','a','a','a','a'];
$count = [];
$crt = -1;
foreach($array ?? [] as $element) {
if($crt == -1 || $count[$crt]["element"] != $element) {
$count[++$crt] = ["element" => $element, "counted_value" => 1];
} else {
$count[$crt]["counted_value"]++;
}
}
The array looks like:
Array
(
[0] => Array
(
[element] => a
[counted_value] => 1
)
[1] => Array
(
[element] => b
[counted_value] => 1
)
[2] => Array
(
[element] => a
[counted_value] => 3
)
[3] => Array
(
[element] => c
[counted_value] => 2
)
[4] => Array
(
[element] => b
[counted_value] => 2
)
[5] => Array
(
[element] => a
[counted_value] => 4
)
)
Compare 2 elements by iterating the array, if both the elements are the same, increase the count else keep the count as 1
$index = 0;
$finalArr[$index] = array('ele'=>$array[0],'cnt'=>1);
for($i=1; $i<count($array); $i++)
{
if($array[$i]===$array[$i-1])
{
++$finalArr[$index]['cnt'];
}
else
{
$finalArr[++$index] = array('ele'=>$array[$i],'cnt'=>1);
}
}
print_r($finalArr);
Let's say I have this set of arrays as input:
[
0 => [1,2,4,5],
1 => [2,3,4],
2 => [1,3],
]
I would like to find all permutations possible selecting one value from each array. That value would be unique in the final result, so it won't repeat. For example, I can't have 1 twice in the result.
The count of arrays on input is the same as count of arrays on output.
Examples of combinations wanted (key=>value):
[0 => 1,1 => 2,2 => 3]
[0 => 2,1 => 3,2 => 1]
[0 => 5,1 => 2,2 => 1]
[0 => 1,1 => 3,2 => null]
Wrong results
[0 => 1,1 => 2,2 => 1]
or
[0 => 2,1 => 2,2 => 3]
I would like to get set of all possible permutations using PHP. How can I do that?
I have attached real data set http://pastebin.com/U6Hyawm4 However, I have no idea how many permutations there may be.
Here's a non-recursive version that's also optimized
/**
* Generates all the possible unique N-tuples from an array of N arrays of integers
*
* #param array $input
* #return array
*/
function generateCombinations(array &$input) {
// since the example results included [1, 3, null] I have assumed that
// null is a possible value of each set.
$sets = [];
foreach($input as $set) {
if(!in_array(null, $set)) {
$set[] = null;
}
$sets[] = $set;
}
// by working on the iterators of each array this loop
// linearizes the entire set of possible combinations
// and iterates it (skipping as many as it can).
$output = [];
$setCount = count($sets);
while(current($sets[0]) !== false) {
$testCombo = [];
for($setIdx = 0; $setIdx < $setCount; $setIdx++) {
if(!in_array(current($sets[$setIdx]), $testCombo)) {
$testCombo[] = current($sets[$setIdx]);
}
else {
// when a combination is thrown out as duplicate
// iterate to skip any other combo's that would also
// contain that duplicate
iterateSets($sets, $setIdx);
break;
}
}
// if there were no duplicates add it to the output and iterate
if(count($testCombo) == $setCount) {
$output[] = $testCombo;
iterateSets($sets, $setCount - 1);
}
}
return $output;
}
/**
* Iterates to the next potentially valid combination. I think of
* this like doing long-hand addition. Add 1 and carry is akin to
* next and reset.
*
* #param array $sets
* #param $index
*/
function iterateSets(array &$sets, $index) {
// reset iterators of all sets past the current one to skip
// combos that cannot be valid
for($i = $index + 1, $ic = count($sets); $i < $ic; $i++) {
reset($sets[$i]);
}
// always move one on current set
next($sets[$index]);
while($index > 0 && current($sets[$index]) === false) {
// wrap if current set is at the end
reset($sets[$index]);
$index--;
// move one for the preceding set
next($sets[$index]);
// then repeat
}
}
The resulting array is:
[
[1,2,3]
[1,2,null]
[1,3,null]
[1,4,3]
[1,4,null]
[1,null,3]
[2,3,1]
[2,3,null]
[2,4,1]
[2,4,3]
[2,4,null]
[2,null,1]
[2,null,3]
[4,2,1]
[4,2,3]
[4,2,null]
[4,3,1]
[4,3,null]
[4,null,1]
[4,null,3]
[5,2,1]
[5,2,3]
[5,2,null]
[5,3,1]
[5,3,null]
[5,4,1]
[5,4,3]
[5,4,null]
[5,null,1]
[5,null,3]
[null,2,1]
[null,2,3]
[null,3,1]
[null,4,1]
[null,4,3]
]
Here's an inefficient version:
$input = array(
[1,2,4,5],
[2,3,4],
[1,3]
);
function appendUnique($subs, $i) {
global $input;
if ($i == count($input)) {
return $subs;
}
$output = array();
foreach ($subs as $sub) {
foreach ($input[$i] as $v) {
$new_sub = array_values($sub);
if (in_array($v, $sub)) {
$new_sub[] = null;
} else {
$new_sub[] = $v;
}
$output[] = $new_sub;
}
}
return appendUnique($output, $i+1);
}
$output = appendUnique([[]], 0);
$output_json = array();
foreach ($output as $row) {
$output_json[] = json_encode($row);
}
$output_json = array_unique($output_json);
$deduped = array();
foreach ($output_json as $json) {
$deduped[] = json_decode($json);
}
print_r($deduped);
outputs:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] =>
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 3
[2] =>
)
[3] => Array
(
[0] => 1
[1] => 4
[2] =>
)
[4] => Array
(
[0] => 1
[1] => 4
[2] => 3
)
[5] => Array
(
[0] => 2
[1] =>
[2] => 1
)
[6] => Array
(
[0] => 2
[1] =>
[2] => 3
)
[7] => Array
(
[0] => 2
[1] => 3
[2] => 1
)
[8] => Array
(
[0] => 2
[1] => 3
[2] =>
)
[9] => Array
(
[0] => 2
[1] => 4
[2] => 1
)
[10] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[11] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
[12] => Array
(
[0] => 4
[1] => 2
[2] => 3
)
[13] => Array
(
[0] => 4
[1] => 3
[2] => 1
)
[14] => Array
(
[0] => 4
[1] => 3
[2] =>
)
[15] => Array
(
[0] => 4
[1] =>
[2] => 1
)
[16] => Array
(
[0] => 4
[1] =>
[2] => 3
)
[17] => Array
(
[0] => 5
[1] => 2
[2] => 1
)
[18] => Array
(
[0] => 5
[1] => 2
[2] => 3
)
[19] => Array
(
[0] => 5
[1] => 3
[2] => 1
)
[20] => Array
(
[0] => 5
[1] => 3
[2] =>
)
[21] => Array
(
[0] => 5
[1] => 4
[2] => 1
)
[22] => Array
(
[0] => 5
[1] => 4
[2] => 3
)
)
This question already has answers here:
PHP Array Group by one field and Sum up two fields [duplicate]
(2 answers)
Closed 4 months ago.
My situation is similar to this thread :
Associative array, sum values of the same key
However in my case all keys are number.
I would like to reduce / combine array where key 0 is similar and make a sum of all other keys.
Here is my original array :
Array
(
[0] => Array
(
[0] => 093042
[1] => 3
[2] => 0
[4] => 0
)
[1] => Array
(
[0] => 222032
[1] => 0
[2] => 13
[4] => 0
)
[2] => Array
(
[0] => 222032
[1] => 0
[2] => 0
[4] => 15
)
[3] => Array
(
[0] => 152963
[1] => 45
[2] => 0
[4] => 0
)
[4] => Array
(
[0] => 222032
[1] => 0
[2] => 7
[4] => 0
)
)
and here is the output i need :
Array
(
[0] => Array
(
[0] => 093042
[1] => 3
[2] => 0
[4] => 0
)
[1] => Array
(
[0] => 222032
[1] => 0
[2] => 20
[4] => 15
)
[2] => Array
(
[0] => 152963
[1] => 45
[2] => 0
[4] => 0
)
)
The solution of other thread is not working because they use the key name and i don't know how i can adapt this to my situation.
Please if you can give me an example of working solution.
REPLY :
For now i try something like that : Take from other thread
$sum = array_reduce($data, function ($a, $b) {
if (isset($a[$b[0]])) {
$a[$b[0]]['budget'] += $b['budget'];
}
else {
$a[$b[0]] = $b;
}
return $a;
});
But this example look is only for key named budget but in my case is number and i have 3 key [1] [2] [3] how can't sum key 1,2,4 where key 0 is similar
This should work for you:
Basically I just loop through your array and check if there is already an element in $result with the key of the first element of $v. If not I initialize it with an array_pad()'ed array of 0's + the current array of the iteration of the foreach loop.
And after this I loop through each element of $v expect the first one and add it to the result array.
At the end I just reindex the result array with array_values().
<?php
foreach($arr as $v){
if(!isset($result[$v[0]]))
$result[$v[0]] = array_pad([$v[0]], count($v), 0);
$count = count($v);
for($i = 1; $i < $count; $i++)
$result[$v[0]][$i] += $v[$i];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => 093042
[1] => 3
[2] => 0
[3] => 0
)
[1] => Array
(
[0] => 222032
[1] => 0
[2] => 20
[3] => 15
)
[2] => Array
(
[0] => 152963
[1] => 45
[2] => 0
[3] => 0
)
)
try this
$data = Array
(
0 => Array
(
0 => 93042,
1 => 3,
2 => 0,
4 => 0,
),
1 => Array
(
0 => 222032,
1 => 0,
2 => 13,
4 => 0,
),
2 => Array
(
0 => 222032,
1 => 0,
2 => 0,
4 => 15,
),
3 => Array
(
0 => 152963,
1 => 45,
2 => 0,
4 => 0,
),
4 => Array
(
0 => 222032,
1 => 0,
2 => 7,
4 => 0,
),
);
var_dump($data);
// grouping
$tab1 = array();
foreach ($data as $e) {
if (!isset($tab1[$e[0]])) {
$tab1[$e[0]] = array();
}
$tab1[$e[0]][] = $e;
}
//var_dump($tab1);
// summing
$tabSum = array();
foreach ($tab1 as $id => $t) {
foreach ($t as $e) {
unset($e[0]);
if (!isset($tabSum[$id])) {
$tabSum[$id] = $e;
} else {
foreach ($e as $key => $value) {
$tabSum[$id][$key] += $value;
}
}
}
}
var_dump($tabSum);
I've got some array of 0-1 elements like:
array('0,0'=>0,'1,0'=>0,'2,0'=>0,'3,0'=>1,'0,1'=>1,'1,1'=>1,'2,1'=>0,'3,1'=>0,'0,2'=>1,'1,2'=>1,'2,2'=>1,'3,2'=>1);
It looks like:
0 0 0 1
1 1 0 0
1 1 1 1
How to find all islands of 1?
In this case i would like to return values:
$island[] = array('3,0');
$island[] = array('0,1','1,1','0,2','1,2','2,2','3,2');
Of course in real cases are more expanded.
Thanks in advance.
$islands = [
[0, 0, 0, 1],
[1, 1, 0, 0],
[1, 1, 1, 1],
];
$result = [];
array_walk_recursive(
$islands,
function ($value, $key) use (&$result) {
static $c = PHP_INT_MAX;
static $r = -1;
if ($c >= $key) {
++$r;
$c = $key;
}
if ($value)
$result[] = $key . ',' . $r;
}
);
var_dump($result);
You can use a foreach inside another foreach to trawl the arrays for values of 1 like this:
<?php
$array=array(
array(0,0,0,1),
array(1,1,0,0),
array(1,1,1,1)
);
$island=array();
foreach($array as $x=>$arr)
{
foreach($arr as $y=>$val)
{
if($val==1)
{
$island[]=array($x, $y);
}
}
}
print_r($island);
?>
with the output:
Array
(
[0] => Array
(
[0] => 0
[1] => 3
)
[1] => Array
(
[0] => 1
[1] => 0
)
[2] => Array
(
[0] => 1
[1] => 1
)
[3] => Array
(
[0] => 2
[1] => 0
)
[4] => Array
(
[0] => 2
[1] => 1
)
[5] => Array
(
[0] => 2
[1] => 2
)
[6] => Array
(
[0] => 2
[1] => 3
)
)
This output finds all indexes of 1 and returns them as a a single entry in the array.
You can also use:
<?php
$array=array(
array(0,0,0,1),
array(1,1,0,0),
array(1,1,1,1)
);
$island=array();
$temp=array();
foreach($array as $x=>$arr)
{
foreach($arr as $y=>$val)
{
if($val==1)
{
$temp[]="'".$x.','.$y."'";
}
}
if(count($temp)>0)
{
$island[]=implode(',',$temp);
}
$temp=array();
}
print_r($island);
?>
Which will make an array closer to your required output:
Array
(
[0] => '0,3'
[1] => '1,0','1,1'
[2] => '2,0','2,1','2,2','2,3'
)