Group Array By Range Value - php

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.

Related

How can I remove filtered values from a Laravel collection within a for loop?

I would basically like to remove values from $collection to reduce the for loop duration since the filtered values are used only once.
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
foreach ($collection as $k => $v) {
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
})->values();
// do stuff with $filtered
break;
}
Results that I'd like to get:
$collection = [1, 2];
$filtered = [3, 4, 5, 6, 7, 8, 9, 10];
What I'm getting currently:
$collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$filtered = [3, 4, 5, 6, 7, 8, 9, 10];
I've tried:
foreach ($collection as $k => $v) {
$filtered = $collection->filter(function ($value, $key) use ($collection) {
$collection->forget($key);
return $value > 2;
})->values();
}
Help would be appreciated.
You can try this :
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$filtered = $collection->filter(function ($value, $key) use ($collection) {
if ($value > 2) {
$collection->forget($key);
return $value;
}
});

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

Given two arrays combine common values

$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
The indexes of $names and $money correspond to each other.
I need to find the most efficient way to add common $money to each $names and print only the even values.
For example, john appears two times, with 2 and 3 money. So johnhas 5 money.
Desired output:
mark: 20
brian: 8
paul: 6
I am thinking of looping through each array, but this is O(n^2) Is there an easier way?
Make associative array (hash) for result:
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$result = [];
for ($i = 0; $i < count($names); $i++) {
if (!isset($result[$names[$i]])) {
$result[$names[$i]] = 0;
}
$result[$names[$i]] += $money[$i];
}
arsort($result); // reverse (big first) sort by money
print_r($result);
just loop through names and use it keys for money array:
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$res = [];
foreach ($names as $key => $value) {
$res[$value] = isset($res[$value]) ? $res[$value] + $money[$key] : $money[$key];
}
var_dump($res);
You just have to loop through $names once and set name as key in a result- variable.
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$data = [];
foreach ($names as $key => $name) {
if (!isset($data[$name])) {
$data[$name] = 0;
}
$data[$name] += $money[$key];
}
print_r($data);
Maybe you'll need a check, if both arrays have the same size.
$names = ['john','brian','john','steven','michael','paul','mark','paul','brian'];
$money = [2, 4, 3, 7, 5, 8, 20, -2, 4];
$result = [];
foreach ($names as $index => $name) {
if (empty($result[$name]) {
$result[$name] = $money[$index];
} else {
$result[$name] += $money[$index];
}
}
print_r($result);

PHP compare arrays

I have two arrays. $arrayOld and $arrayNew and I want to compare these arrays and only select the values that are not in $arrayNew.
I don't want the values that are in $arrayNew only. So I don't think array_diff() is gonna help me.
$arrayOld = [1, 2, 3, 4, 5]
$arrayNew = [1, 4, 5, 6, 7]
So it only needs to return 2 and 3 and not 6 or 7.
use array_diff, to accomplish this. As you need to difference between the array and need data from Old array so you need to use the old array as the first parameter of the array_diff.
Note: Array diff only returns from the first array which is not in second array.
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
$n = array_diff($arrayOld, $arrayNew);
print_r($n);
Result: Online Check
Array
(
[1] => 2
[2] => 3
)
If you need a new keys for the output array just use array_values. The new key start from 0.
$arr = array_values($n);
Use below code
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
print "<pre>";
print_r(array_diff($arrayOld, $arrayNew));
OUTPUT:
Array
(
[1] => 2
[2] => 3
)
use this code.
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));
$compare = array();
$i=1;
foreach($arrayOld as $k=>$v){
if(!in_array($v, $arrayNew)){
$compare[$i] = $v;
$i++;
}
}
$i=$i;
foreach($arrayNew as $k=>$v){
if(!in_array($v, $arrayOld)){
$compare[$i] = $v;
$i++;
}
}
use array_diff function
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));

remove elements in php array that occur more than twice

I am looking for a function in php,where the function must delete values in the array that shows up three times or more? For example, if you give the function array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0) the funciton will return array(2, 4, 6, 3, 4, 2, 0)
You can use array_count_values() to get frequencies. Then use a foreach to get values that has frequency less than 3...
$array = array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0);
$frq = array_count_values($array);
$result = array();
foreach ($frq as $key=>$value){
if ($value < 3){
$result[] = $key;
}
}
function FilterMyArray(array &$array){
$array_count=array_count_values($array);
foreach($array_count as $key => $value){
if($value > 2){
foreach (array_keys($array, $key, true) as $unsetKey) {
unset($array[$unsetKey]);
}
}
}
}
$array=array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
FilterMyArray($array);
print_r($array);
Output
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 2 [7] => 3 [8] => 1 [9] => 9 )
`
This will remove all duplicates, but you'd have to add to it to count the number of each of the values.
$a = array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0);
$b = array();
for ($a as $key=>$value) {
if (!in_array($value, $b)) {
$b[] = $value;
}
}
// array $b has all the values with no duplicates.

Categories