How to Create matrix of dynamic arrays into one array? - php

I have dynamic number of arrays like:
$a= [ 1, 2 ];
$b= [ 3, 4, 5 ];
$c= [ 6 ];
I want to merge every number from each row with parent values sustaining till all child values complete. So starting from lowest level, the matrix gets created as follows:
Third level:
[ 6 ]
Second level:
[
[3 , 6] ,
[4 , 6] ,
[5 , 6]
]
First/Top level(final level):
[
[1, 3, 6] ,
[1, 4 , 6] ,
[1, 5 , 6] ,
[2, 3, 6] ,
[2, 4 , 6] ,
[2, 5 , 6]
]
Note: The number of arrays can vary and the number of elements in each array might also change.
I want a way to get those into matrix like above in the final result. Any ideas on how to get started?

I've had the same problem before , here what tried.
1-You can use crossJoin helper with collection
$results=collect($a)->crossJoin($b,$c);
2-You can use some thing native like this
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = array();
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
dd(
combinations(
array(
array('1','2','3'),
array('4','5','6'),
array('7','8')
)
)
);

Related

PHP: Nested loop with only unique pairs

I need to create an array of only unique pairs so there are no duplicates.
The problem is that when just iterating over each other, for some reason last pair usually is duplicate.
So here's a quick example:
<?php
$teams = [
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
];
foreach ($teams as $team_a) {
foreach ($teams as $team_b) {
if ($team_a['id'] !== $team_b['id']) {
$pairs[] = [$team_a['id'], $team_b['id']];
}
}
}
?>
Returns something like this:
0: [1, 2]
1: [1, 3]
2: [1, 4]
3: [2, 1]
4: [2, 3]
5: [2, 4]
6: [3, 1]
7: [3, 2]
8: [3, 4]
...
So you can see that some pairs are the same, like [1, 2] and [2, 1]. And after the half iterations there are only repeats.
What would be the most efficient way to iterate like this and be sure that there are only unique pairs?
Thanks!
Obviously the duplicate always appears when key(team_a) > key(team_b)
<?php
$teams = [
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
];
foreach ($teams as $offset => $team_a) {
foreach (array_slice($teams, $offset+1) as $team_b) {
$pairs[] = [$team_a['id'], $team_b['id']];
}
}
You could to do this with recursion instead of two for each loops.
<?php
$teams = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10];
getMatchup($teams);
function getMatchUp($teams, $matches = [], $start = 0) {
// Check if the whole array has been checked
if ($start == count($teams)) {
return var_dump($matches);
}
// Check every option considering a certain start point
for ($x = $start; $x < count($teams); $x++) {
// As long the team is not the same, add to matches
if ($start !== $x) {
$matches[] = [$teams[$start] => $teams[$x]];
}
}
// First team has been matched up, start matching the second team and so on..
getMatchup($teams,$matches, $start + 1);
}
Running example:
http://sandbox.onlinephpfunctions.com/code/4277d966c4ceacdbe7024d14fb03fe05fd760471

how to merge array? array_merge not working

hello everybody im trying to merge a a few array in an array.
currently with my code:
$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
foreach ($matchs as $key => $match) {
$array[] = [
$match->date_access => $match->status,
];
}
dd($array);
so with this i get the ouput when i dd(); like:
so what i want to do now is to merge all that array to become one in array:16>
itself.
how can i do that? i have tried array merge and it isnt working either
For your case you should have a unique $match->date_access so you can use it as a key of your array, like this :
$matchs = DiraChatLog::where('status','=','Match')
->whereBetween('date_access', [$request->from, $request->to])
->get();
foreach ($matchs as $key => $match) {
$array[$match->date_access] = $match->status;
}
if you have a more complex data you can use array_collapse helper to collapses an array of arrays into a single array,
here is an example :
$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

Group Array By Range Value

I have this array [1,1,2,2,2,3,4,4,5,6,6,6,7], may I group the array according to the range value, so get the final result:
'1-3' = [1,1,2,2,2,3]; // Count is 6
'4-5' = [4,4,5]; // Count is 3
'6-7' = [6,6,6,7]; // Count is 4
What you need I believe is:
function array_get_range($array, $min, $max) {
return array_filter($array, function($element) use ($min, $max) {
return $element >= $min && $element <= $max;
});
}
$array = [1,1,2,2,2,3,4,4,5,6,6,6,7];
$range13 = array_get_range($array, 1, 3); // [1, 1, 2, 2, 2, 3]
$range45 = array_get_range($array, 4, 5); // [4, 4, 5]
$range67 = array_get_range($array, 6, 7); // [6, 6, 6, 7]
Create a new array with your ranges, then iterate through the values and through the ranges inside. If the current value is inside the range, add the record to the current range:
<?php
$numbers = [1,1,2,2,2,3,4,4,5,6,6,6,7];
$counts = [];
$counts[] = ["values"=> [1, 3], "records" => []]; // first value in "values" is min, second is max
$counts[] = ["values"=> [4, 5], "records" => []];
$counts[] = ["values"=> [6, 7], "records" => []];
foreach ($numbers as $number) {
foreach ($counts as $key => &$value) {
if ($number >= $value["values"][0] && $number <= $value["values"][1]) { // if between the range, add it to the records
$value["records"][] = $number;
}
}
}
echo "<pre>";
foreach ($counts as $count) {
echo $count["values"][0]." - ".$count["values"][1]." = ".count($count["records"])." elements<br>";
}
Demo
I think array_intersect() and range() with sizeof()/count() does a cleaner job for this task. It eliminates the double-conditional.
Code: (Demo)
function count_range($array, $min, $max) {
return sizeof(array_intersect($array, range($min, $max)));
}
$array = [1, 1, 2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7];
echo count_range($array, 1, 3); // 6 from [1, 1, 2, 2, 2, 3]
echo count_range($array, 4, 4); // 2 from [4, 4]
echo count_range($array, 2, 7); // 11 from [2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7]
range() creates a whitelist array of one or more consecutive integers.
array_intersect() compares the input array and the whitelist array all at once.
sizeof() is just an alias of count() to determine how many elements were retained.

Merge two flat arrays and omit values from one array when the other array has more occurrences of the same value

I want to merge every element of two arrays, BUT if a value is in both arrays, then only add the values from the array which has the biggest amount of that element. The result array does not need to be sorted in any special way, but I did it here for readability.
Sample input:
$array1 = [1, 4, 7, 3, 3, 3];
$array2 = [4, 0, 3, 4, 9, 9];
Desired result:
[0, 1, 3, 3, 3, 4, 4, 7, 9, 9]
//a2 a1 a1 a1 a1 a2 a2 a1 a2 a2
Note, this will be used on big arrays, with unknown integer values. Is there a good way to do this that doesn't require too much time/processing power?
Try this:
<?php
$array1 = [1, 4, 7, 3, 3, 3];
$array2 = [4, 0, 3, 4, 9, 9];
function min_merge($arr1, $arr2) {
$arr1 = array_count_values($arr1);
$arr2 = array_count_values($arr2);
foreach ($arr2 as $index => $arr)
if (!isset($arr1[$index]) || $arr > $arr1[$index])
$arr1[$index] = $arr;
foreach ($arr1 as $index => $arr)
for ($i = 0; $i < $arr; $i++)
$final[] = $index;
return $final;
}
print_r(min_merge($array1, $array2));
Output:
Array (
[0] => 1
[1] => 4
[2] => 4
[3] => 7
[4] => 3
[5] => 3
[6] => 3
[7] => 0
[8] => 9
[9] => 9
)
Unsorted, but it contains all the numbers from [0, 1, 3, 3, 3, 4, 4, 7, 9, 9].
$count[0] = array_count_values($arr1);
$count[1] = array_count_values($arr2);
$out = array();
array_map(function($e) use(&$out, $count){
$n1 = (isset($count[0][$e])) ? $count[0][$e] : 0;
$n2 = (isset($count[1][$e])) ? $count[1][$e] : 0;
$next = ($n2 > $n1) ? array_fill(0, $n2, $e) : array_fill(0, $n1, $e);
$out = array_merge($out, $next);
}, array_keys($count[0] + $count[1]));
print_r($out);
My modernized rewrite of #DaveChen's answer using PSR-12 coding standards and eliminating single-use declarations. This approach uses one loop to determine the greater count for numbers shared by both value-count arrays, then a second loop to populate the result array. (Demo)
$counts1 = array_count_values($array1);
foreach (array_count_values($array2) as $number => $count) {
if ($count > ($counts1[$number] ?? 0)) {
$counts1[$number] = $count;
}
}
$result = [];
foreach ($counts1 as $number => $count) {
array_push($result, ...array_fill(0, $count, $number));
}
var_export($result);
My modernized rewrite of #Expedito's answer which does not abuse the array_map() (when array_map()'s return value is not used, use array_walk() for functional style programming), uses a foreach() loop to eliminate variable scope issues, and generally implements D.R.Y. techniques. (Demo)
$counts1 = array_count_values($array1);
$counts2 = array_count_values($array2);
$result = [];
foreach ($counts1 + $counts2 as $num => $cnt) {
array_push(
$result,
...array_fill(
0,
max($counts1[$num] ?? 0, $counts2[$num] ?? 0),
$num
)
);
}
var_export($result);
And I wanted to add a new approach of my own, despite the fact that it may or may not perform better than the other two snippets. The script makes one pass over the first value count arrays to populate a temporary array which demands which numbers from the first array should be represented in the result array. Then it isolates value intersections from the first array, value differences from the second array, then merges them. (Demo)
$counts1 = array_count_values($array1);
$counts2 = array_count_values($array2);
$keepFrom1 = array_keys(
array_filter(
$counts1,
fn($count, $number) => ($counts2[$number] ?? 0) <= $count,
ARRAY_FILTER_USE_BOTH
)
);
var_export(
array_merge(
array_intersect($array1, $keepFrom1),
array_diff($array2, $keepFrom1)
)
);
probably not the most optimized but
<?php
$one=[1, 4, 7, 3, 3, 3];
$two=[4, 0, 3, 4, 9, 9];
sort($one);
sort($two);
foreach($one as $el)
{
$combined[]=$el;
if (array_search($el,$two))
{
unset($two[array_search($el,$two)]);
}
}
foreach($two as $el)
{
$combined[]=$el;
}
sort($combined);
print_r($combined);
?>

Find all possible unique combinations of elements of an array in PHP [duplicate]

This question already has answers here:
Finding the subsets of an array in PHP
(5 answers)
Closed 9 years ago.
I’m aware of several questions covering this topic (e.g. here), but none of them (at least from what I found) do what I need.
Say I have an array of 3 elements [1, 2, 3]. I need to find all the possible unique combinations (so excluding permutations, like here), including the ones of repeating elements. So the result should be:
[1]
[2]
[3]
[1, 1]
[1, 2]
[1, 3]
[2, 2]
[2, 3]
[3, 3]
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 2]
[1, 2, 3]
[1, 3, 3]
[2, 2, 2]
[2, 2, 3]
[2, 3, 3]
[3, 3, 3]
Excluding subsets like [3, 2, 1] or [2, 1, 3], that are the same thing as [1, 2, 3].
How can I achieve this?
Fast solution using recursion, probably not the best way to do it, but it gets the job done.
<?php
$arr = array(1,2,3);
$result = array();
function combinations($arr, $level, &$result, $curr=array()) {
for($i = 0; $i < count($arr); $i++) {
$new = array_merge($curr, array($arr[$i]));
if($level == 1) {
sort($new);
if (!in_array($new, $result)) {
$result[] = $new;
}
} else {
combinations($arr, $level - 1, $result, $new);
}
}
}
for ($i = 0; $i<count($arr); $i++) {
combinations($arr, $i+1, $result);
}
// TEST
foreach ($result as $arr) {
echo join(" ", $arr) . '<br>';
}
?>

Categories