I have this array with year as its key and count as its value,
$dateRange = array([
"1990" => 10,
"1991" => 12,
"1992" => 12,
"1993" => 12,
"1994" => 12,
"1995" => 12,
"1996" => 6
]);
and how can I make it look like this *(getting consecutive year/s with a count/value of 12 then count its total value)
$dateRange = ([
"1990" => 10,
"1991-1995" => 60,
"1996" => 6
]);
I came up with the following solutions, it's not efficient with large data but will work.
1991-1995 will be string though.
$dateRange = array([
1990 => 10,
1991 => 12,
1992 => 12,
1993 => 12,
1994 => 12,
1995 => 12,
1996 => 6
]);
dump($dateRange[0]);
$first = reset($dateRange[0]);
$last = end($dateRange[0]);
$newArr = array([]);
$sum = 0;
$firstRange = null;
$lastRange = null;
foreach($dateRange[0] as $key => $item){
if($item == $first){
$newArr[0][$key] = $item;
}
elseif($item == $last){
$newArr[0][$firstRange.'-'.$lastRange] = $sum;
$newArr[0][$key] = $item;
}
else{
if($firstRange == null){
$firstRange = $key;
}
$lastRange = $key;
$sum += $item;
}
}
dd($newArr[0]);
Loop through each individual dataset(subarray) and
Sort the keys numerically.
Compare every current key with previous key to check if they are consecutive.
If prev_key + 1 == curr_key, then they are consecutive, else they are not.
Check the same for their values' equality as well.
In either step 2 or 3, if any of the conditions don't match, add the current range and it's sum to the result and start a new range from this index. Likewise, collect all the results.
Snippet:
<?php
$res = [];
foreach($dateRange as $k => $v){
$temp = [];
$keys = array_keys($v);
sort($keys, SORT_NUMERIC);
$prev = 0;
$sum = 0;
for($i = 0; $i < count($keys); ++$i){
if($i > 0 && ($keys[ $i - 1 ] + 1 != $keys[ $i ] || $v[ $keys[ $i - 1 ] ] !== $v[ $keys[ $i ] ])){
if($prev == $i - 1) $temp[ $keys[ $prev ]] = $sum;
else $temp[ $keys[$prev] . "-" . $keys[$i - 1]] = $sum;
$sum = 0;
$prev = $i;
}
$sum += $v[$keys[ $i ]];
if($i === count($keys) - 1){
if($prev == $i) $temp[ $keys[ $prev ]] = $sum;
else $temp[ $keys[$prev] . "-" . $keys[ $i ]] = $sum;
}
}
$res[] = $temp;
}
print_r($res);
Online Demo
Here is a more Laravel friendly solution using Framework collection methods.
Using Collection is more readable and more understandable.
$dateRange = [
[
"1990" => 10,
"1991" => 12,
"1992" => 12,
"1993" => 12,
"1994" => 12,
"1995" => 12,
"1996" => 6,
],
];
collect($dateRange)->map(function ($range)
{
return collect($range)->groupBy(function ($item)
{
return $item;
}, true)->mapWithKeys(function (Collection $item)
{
$sorted = $item->sortKeys();
$keys = $sorted->keys();
$firstYear = $keys->first();
$lastYear = $keys->last();
$sum = $item->sum();
return [($firstYear != $lastYear ? $firstYear.'-'.$lastYear : $firstYear) => $sum];
});
})->dd();
Output:
array:1 [▼
0 => Illuminate\Support\Collection {#2259 ▶
#items: array:3 [▶
1990 => 10
"1991-1995" => 60
1996 => 6
]
#escapeWhenCastingToString: false
}
]
$dateRange = [
'1990' => 10,
'1991' => 12,
'1992' => 12,
'1993' => 12,
'1994' => 12,
'1995' => 12,
'1996' => 6
];
ksort($dateRange);
$previousValue = '';
$result = [];
array_walk(
$dateRange,
function (int $value, string $key) use (&$previousValue, &$result) {
if ($previousValue === $value) {
$lastKey = array_key_last($result);
$result[substr($lastKey, 0, 4) . '-' . $key] = $result[$lastKey] + $value;
unset($result[$lastKey]);
} else {
$result[$key] = $value;
}
$previousValue = $value;
},
[]
);
print_r($result);
You have an array of arrays here. You could just do $dateRange = [...]; but I'll assume this is what you intended.
$dateRange = array([
"1990" => 10,
"1991" => 12,
"1992" => 12,
"1993" => 12,
"1994" => 12,
"1995" => 12,
"1996" => 6
]);
Using Collection methods, it should be easy.
$collection = collect($dateRange[0]);
$keys = $collection->keys();
$newKeys = [
$keys->shift(), // remove and return first key of $keys (1990)
$keys->pop(), // remove and return last key of $keys (1996)
"{$keys->first()}-{$keys->last()}", // concat first key and last key of $keys ("1991-1995")
];
// $newKeys is now equal to [1990, "1991-1995", 1996]
$newValues = [
0 => $collection->pull($newKeys[0]), // remove and return element 1990 from collection (10)
2 => $collection->pull($newKeys[1]), // remove and return element 1996 from collection (6)
1 => $collection->sum(), // return sum of remaining elements in collection (60)
];
ksort($newValues); // sort $newValues by its key (this is why I specified them in the order 0, 2, 1).
// $newValues is equal to [10, 60, 6]
$newArray = array_combine($newKeys, $newValues); // create a new array from $newKeys and $newValues
Related
I have two array
First
array(
0 => 100000,
1 => 50000,
2 => 100000,
3 => 100000);
Second
array(
0 => 150000,
1 => 200000,);
The problem is I want to get the combination from first array that formed each second array.
Example
Second array index 0 can be formed from first array index 0 and 1 and second array index 1 can be formed from first array index 2 and 3
I want to achieve like this
[0 => [0,1] , 1 => [2,3]]
Thanks for the help.
A simple an fast gready approach would be to first sort the arrays descending. After that, loop over the second one and collect from the first one as much values, till you reach your desired value.
$first = [
0 => 100000,
1 => 50000,
2 => 100000,
3 => 100000
];
$second = [
0 => 150000,
1 => 200000
];
arsort($first);
arsort($second);
$combinations = [];
foreach ($second as $search) {
$combination = [];
$sum = 0;
foreach ($first as $key => $val) {
if ($sum + $val > $search) continue;
$sum += $val;
$combination[] = $key;
if ($sum == $search) break;
}
if ($sum != $search) die("nothing found this way..");
foreach ($combination as $val) unset($first[$val]);
$combinations[] = $combination;
}
print_r($combinations);
Here is the simple idea to achieve your requirement. There would be many if's and but you need to consider yourself
<?php
$arr1 = [0 => 100000,1 => 50000,2 => 100000,3 => 100000];
$arr2 = [0 => 150000,1 => 200000];
//$arr2 = [0 => 250000,1 => 100000];
$count = 0;
$new = array();
for($i=0;$i<count($arr1);$i++){
if($arr1[$i] == $arr2[$count]){
$new[$arr2[$count]] = $arr1[$i];
$count++;
}else{
$sum = [$arr1[$i]];
for($j=$i+1;$j<count($arr1);$j++){
$sum[] = $arr1[$j];
if( array_sum($sum) == $arr2[$count]){
$new[$arr2[$count]] = $sum;
$count++;
}
}
}
}
print_r($new);
?>
Output1
Output2
Try with following code:
$arr = array(
0 => 100000,
1 => 50000,
2 => 100000,
3 => 100000);
$count = $val = 0;
$newArr = [];
foreach($arr as $a){
$val += $a;
$count++;
if($count == 2){
$newArr[] = $val;
$val = $count = 0;
}
}
print_r($newArr); die;
I am trying to get this function working by adding one point at at time, till there are no more.
What I am trying to do is, getting it to increment "Torsk" value by one, then go to the next, increment by one, and then so on an on, till all the points set in $prize have been distributed.
$prize = [
"Torsk" => 0,
"Rødspætte" => 0,
"Mørksej" => 0,
"Skrubbe" => 0,
"Lubbe/Lyssej" => 0,
"Ising" => 0,
"Lange" => 0,
"Makrel" => 0,
"Hornfisk" => 0,
"Pig-/Slet-hvar" => 0,
"Havkat" => 0,
"Laks/Havørred" => 0,
];
$points = 21;
for ($i = 0; $i < $points; ++$i) {
foreach ($prize as $key => $value) {
$prize[$key]++;
if (array_sum($prize) == $points) continue;
}
}
echo array_sum($prize);
echo "<br />";
print_r($prize);
When it is finished I expected the array to be like this:
$prize = [
"Torsk" => 2,
"Rødspætte" => 2,
"Mørksej" => 2,
"Skrubbe" => 2,
"Lubbe/Lyssej" => 2,
"Ising" => 2,
"Lange" => 2,
"Makrel" => 2,
"Hornfisk" => 2,
"Pig-/Slet-hvar" => 1,
"Havkat" => 1,
"Laks/Havørred" => 1,
];
But it is not, instead it has just added 21 to all of them.
you could use break 2; to break from outer loop when your condition is reached, like:
for ($i = 0; $i < count($prize); ++$i) {
foreach ($prize as $key => $value) {
$prize[$key]++;
if (array_sum($prize) == $points) {
break 2;
}
}
}
Try this code snippet:
$prize = [
"Torsk" => 0,
"Rødspætte" => 0,
"Mørksej" => 0,
"Skrubbe" => 0,
"Lubbe/Lyssej" => 0,
"Ising" => 0,
"Lange" => 0,
"Makrel" => 0,
"Hornfisk" => 0,
"Pig-/Slet-hvar" => 0,
"Havkat" => 0,
"Laks/Havørred" => 0,
];
$points = 21;
foreach ($prize as $key => $value) {
$prize[$key]++;
if (array_sum($prize) == $points) break;
}
if(array_sum($prize) < $points){
foreach ($prize as $key => $value) {
$prize[$key]++;
if (array_sum($prize) == $points) break;
}
}
echo array_sum($prize);
echo "<br />";
print_r($prize);
You should use break 2; instead of continue to break the two nested enclosing structures.
The following is an improved way to do the same task.
<?php
/**
* Disturbe values until the sum is equal to $points
* #param $prize array
* #param $points integer
* #return array|false
*/
function distribute_prize( $prize = array(), $points = 21 ){
$sum = array_sum( $prize );
if( $sum == $points ) return $points;
elseif( $sum > $points || $points < 0 ) return false;
while( true ){
foreach ($prize as $key => $value) {
$prize[$key]++;
if( array_sum( $prize ) == $points ) break 2;
}
}
return $prize;
}
// TEST
$prize = [
"Torsk" => 0,
"Rødspætte" => 0,
"Mørksej" => 0,
"Skrubbe" => 0,
"Lubbe/Lyssej" => 0,
"Ising" => 0,
"Lange" => 0,
"Makrel" => 0,
"Hornfisk" => 0,
"Pig-/Slet-hvar" => 0,
"Havkat" => 0,
"Laks/Havørred" => 0,
];
$points = 21;
print_r( distribute_prize( $prize, $points ) );
i have 7 numbers in random from -100 to 100, from those seven numbers i need to find the biggest diference between each them
example:
-50 , 60 , -4 , 80 , 25, -34, 15
the output will be 130 -50 and 80 is the biggest diference
but i need in php
i have tried echo max(rand(-100, 100));
but it is not the output im looking for
this i tried too:
$myarray = array(
'this' => 2,
'that' => 14,
'them' => -5,
'other' => 200,
'nothing' => 42,
'somethingelse' => 1,
'you' => 10,
'me' => 30);
foreach ($myarray as $key => $value) {
if (!isset ($min) || $value < $min) { $min = $value; }
if (!isset ($max) || $value > $max) { $max = $value; }
}
$diff = $max - $min;
echo $diff;
any suggestion?
<?php
$array = array(-50 , 60 , -4 , 80 , 25, -34, 15);
$min = 0;
$max = 0;
foreach($array as $element) {
if ($element < $min) $min = $element;
if ($element > $max) $max = $element;
}
echo $max - $min;
Your approach using max() wasn’t wrong, but max() expects an array as its first parameter whereas rand() only returns a single integer.
The solution should be:
$diff = max($myarray) - min($myarray);
try this one
$myarray = array(
'this' => 2,
'that' => 14,
'them' => -5,
'other' => 200,
'nothing' => 42,
'somethingelse' => 1,
'you' => 10,
'me' => 30);
function bigDiff($arr){
if(count($arr)>0){
$max = max($arr);
$min = min($arr);
$diff = ($max)-($min);
return $diff;
}
return "Null input array provided";
}
echo bigDiff($myarray);
outputs 205
This could be another way:
$arr = array(
'this' => 2,
'that' => 14,
'them' => -5,
'other' => 200,
'nothing' => 42,
'somethingelse' => 1,
'you' => 10,
'me' => 30
);
sort($arr);
$diff = $arr[count($arr) - 1] - $arr[0];
You can try this:
$array = array(-50 , 60 , -4 , 80 , 25, -34, 15);
$max = max($array); //80
$min = min($array); //-50
$difference = $max - $min; //130
I trying to get the minimum values from the any column contains "xx" in the column name.
Below is my code:
<?php
$array = array(
array(
'id' => 1,
'10xx' => 14,
'11xx' => 32,
'12xx' => 4
),
array(
'id' => 2,
'10xx' => 13,
'11xx' => 36,
'12xx' => 41
)
);
foreach($array as $item)
{
$lowestKey = '';
foreach($item as $key => $value)
{
if(strpos($key, 'xx') === 0)
{
if($lowestKey == '')
{
$lowestKey = $key;
}
else
{
if($value < $item[$lowestKey])
{
$lowestKey = $key;
}
}
}
}
echo 'lowest for id ' . $item['id'] . ': ' . $item[$lowestKey] . "\n";
}
?>
You have a function already for it:
http://php.net/manual/en/function.min.php
echo min(2, 3, 1, 6, 7); // 1
echo min(array(2, 4, 5)); // 2
echo min(0, 'hello'); // 0
echo min('hello', 0); // hello
echo min('hello', -1); // -1
Combine it with array_values if this fits better your needs.
function _getNumber($array) {
return $array['id'];
}
$numbers = array_map('_getNumber', $array);
OR
$numbers = array_map(function($array) {
return $array['id'];
}, $array);
echo $min = min($numbers);
echo $max = max($numbers);
function find_lowest($array){
$new_array = array();
foreach($array as $key => $val ){
if(is_array($val)){
$new_array[$key] = find_lowest($val);
}else{
$new_array[$key] = $val ;
}
}
return min($new_array);
}
$array = array( array( 'id' => 1,
'10xx' => 14,
'11xx' => 32,
'12xx' => 4
),
array(
'id' => 2,
'10xx' => 13,
'11xx' => 36,
'12xx' => 41
)
);
echo find_lowest($array);
Instead of looping again inside just use the min() function.
$lowest_keys = array();
foreach($array as $item)
{
unset( $item[ 'id' ] );
$lowest_keys[] = min( $item );
}
Iterate each row/subarray with a foreach() loop or array_walk().
Extract and display the id (first element) value with array_shift().
Call min() on the remaining values in the respective subarray to determine the lowest value.
No conditional expressions. No unnecessary variables. Clean, concise, and effective.
Code: (Demo)
$array = [
['id' => 1, '10xx' => 14, '11xx' => 32, '12xx' => 4],
['id' => 2, '10xx' => 13, '11xx' => 36, '12xx' => 41]
];
array_walk($array, function($row) {
echo array_shift($row) , " : " , min($row) , "\n";
});
Output:
1 : 4
2 : 13
$array = array(
array(
'id' => 14,
'10xx' => 14,
'11xx' => 32,
'12xx' => 4
),
array(
'id' => 2,
'10xx' => 13,
'11xx' => 36,
'12xx' => 41
)
);
$lowestKey = '';
foreach($array as $arra){
foreach ($arra as $key=>$value){
if ($key == 'id'){
if(($value < $lowestKey )||( $lowestKey== '')){
$lowestKey = $value;
}
}
}
}
echo $lowestKey;
I have an array like this:
array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
)
I need to extract minimal and maximal Weight values.
In this example
$min_value = 175
$max_value = 200
Any help on how to do this ?
Thank you !
Option 1. First you map the array to get those numbers (and not the full details):
$numbers = array_column($array, 'weight')
Then you get the min and max:
$min = min($numbers);
$max = max($numbers);
Option 2. (Only if you don't have PHP 5.5 or better) The same as option 1, but to pluck the values, use array_map:
$numbers = array_map(function($details) {
return $details['Weight'];
}, $array);
Option 3.
Option 4. If you only need a min OR max, array_reduce() might be faster:
$min = array_reduce($array, function($min, $details) {
return min($min, $details['weight']);
}, PHP_INT_MAX);
This does more min()s, but they're very fast. The PHP_INT_MAX is to start with a high, and get lower and lower. You could do the same for $max, but you'd start at 0, or -PHP_INT_MAX.
foreach ($array as $k => $v) {
$tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);
For the people using PHP 5.5+ this can be done a lot easier with array_column. Not need for those ugly array_maps anymore.
How to get a max value:
$highest_weight = max(array_column($details, 'Weight'));
How to get the min value
$lowest_weight = min(array_column($details, 'Weight'));
It is interesting to note that both the solutions above use extra storage in form of arrays (first one two of them and second one uses one array) and then you find min and max using "extra storage" array. While that may be acceptable in real programming world (who gives a two bit about "extra" storage?) it would have got you a "C" in programming 101.
The problem of finding min and max can easily be solved with just two extra memory slots
$first = intval($input[0]['Weight']);
$min = $first ;
$max = $first ;
foreach($input as $data) {
$weight = intval($data['Weight']);
if($weight <= $min ) {
$min = $weight ;
}
if($weight > $max ) {
$max = $weight ;
}
}
echo " min = $min and max = $max \n " ;
How about without using predefined functions like min or max ?
//find max
$arr = [4,5,6,1];
$val = $arr[0];
$n = count($arr);
for($i=0;$i<$n;$i++) {
if($val < $arr[$i]) {
$val = $arr[$i];
}
}
echo $val;
//find min
$arr = [4,5,6,1];
$val = $arr[0];
$n = count($arr);
for($i=0;$i<$n;$i++) {
if($val > $arr[$i]) {
$val = $arr[$i];
}
}
echo $val;
$num = array (0 => array ('id' => '20110209172713', 'Date' => '2011-02-09', 'Weight' => '200'),
1 => array ('id' => '20110209172747', 'Date' => '2011-02-09', 'Weight' => '180'),
2 => array ('id' => '20110209172827', 'Date' => '2011-02-09', 'Weight' => '175'),
3 => array ('id' => '20110211204433', 'Date' => '2011-02-11', 'Weight' => '195'));
foreach($num as $key => $val)
{
$weight[] = $val['Weight'];
}
echo max($weight);
echo min($weight);
<?php
$array = array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
);
foreach ($array as $key => $value) {
$result[$key] = $value['Weight'];
}
$min = min($result);
$max = max($result);
echo " The array in Minnumum number :".$min."<br/>";
echo " The array in Maximum number :".$max."<br/>";
?>
$Location_Category_array = array(5,50,7,6,1,7,7,30,50,50,50,40,50,9,9,11,2,2,2,2,2,11,21,21,1,12,1,5);
asort($Location_Category_array);
$count=array_count_values($Location_Category_array);//Counts the values in the array, returns associatve array
print_r($count);
$maxsize = 0;
$maxvalue = 0;
foreach($count as $a=>$y){
echo "<br/>".$a."=".$y;
if($y>=$maxvalue){
$maxvalue = $y;
if($a>$maxsize){
$maxsize = $a;
}
}
}
echo "<br/>max = ".$maxsize;
print fast five maximum and minimum number from array without use of sorting array in php
:-
<?php
$array = explode(',',"78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73");
$t=0;
$l=count($array);
foreach($array as $v)
{
$t += $v;
}
$avg= $t/$l;
echo "average Temperature is : ".$avg." ";
echo "<br>List of seven highest temperatsures :-";
$m[0]= max($array);
for($i=1; $i <7 ; $i++)
{
$m[$i]=max(array_diff($array,$m));
}
foreach ($m as $key => $value) {
echo " ".$value;
}
echo "<br> List of seven lowest temperatures : ";
$mi[0]= min($array);
for($i=1; $i <7 ; $i++)
{
$mi[$i]=min(array_diff($array,$mi));
}
foreach ($mi as $key => $value) {
echo " ".$value;
}
?>