PHP getting count for values that falls within expected ranges - php

I have a list containing a bunch of values from 1-400. I am trying to divide the data into ranges like [1-50], [51-100], .. , [351-400] and get the count for the values that falls within the range given . I basically have the code working. So, my question would be is there a better way to do this or what would be a good practise for this?
$temp = array(); //temp array to store the values from mysql
$final_array = array //final array to store the counts from the range initialized to 0
(
"0" => 0,
"1-50" => 0,
"51-100" => 0,
"101-150" => 0,
"151-200" => 0,
"201-250" => 0,
"251-300" => 0,
"301-350" => 0,
"351-400" => 0
);
$sql = "SELECT count(*) AS total FROM user GROUP BY user_id";
$statement = $DB_->link->prepare ( $sql );
$statement->execute ();
if ($result = $statement->get_result ())
{
while ( $row = $result ->fetch_assoc() )
{
$temp [] = $row;
}
}
else
{
die(mysql_error());
}
foreach ($temp as $child)
{
if( $child['total'] >= 351 && $child['total'] <= 400)
{
$final['351-400']++;
}
elseif( $child['total'] >= 301 && $child['total'] <= 350)
{
$final['301-350']++;
}
...
elseif( $child['total'] >= 1 && $child['total'] <= 50)
{
$final['1-50']++;
}
}
Desired results
Array
(
[0] => 0
[1-50] => 1
[51-100] => 0
[101-150] => 0
[151-200] => 1
[201-250] => 0
[251-300] => 4
[301-350] => 5
[351-400] => 18
)

I would iterate over the keys of final_array, exploding them using - as the delimiter. While this method is slower than what you have, as it's iterating over final_array for each row returned from the database, it is much more maintainable, as it seamlessly handles keys for exact matches (only 1 number), and arbitrary ranges. Adding more buckets simply requires editing the final_array array, rather than changing a bunch of lines of code.
foreach ($temp as $child) {
foreach($final_array as $key => $value) {
if (strpos($key, '-') === false) {
if ($child['total'] == intval($key)) {
$final_array[$key]++;
}
} else {
list($min, $max) = explode('-', $key);
if ($child['total'] >= intval($min) && $child['total'] <= intval($max)) {
$final_array[$key]++;
}
}
}
}
I would also forgo the use of the $temp array, simply processing the results as they are returned:
if ($result = $statement->get_result ())
{
while ( $child = $result->fetch_assoc() ) {
foreach($final_array as $key => $value) {
if (strpos($key, '-') === false) {
if ($child['total'] == intval($key)) {
$final_array[$key]++;
}
} else {
list($min, $max) = explode('-', $key);
if ($child['total'] >= intval($min) && $child['total'] <= intval($max)) {
$final_array[$key]++;
}
}
}
}
}
else
{
die(mysql_error());
}
The most efficient system would load all the results in an array, pass that array through array_count_values, and then aggregate those.

This is the best way which you used & for single key count we use PHP function array_count_values

Related

How get a (fixed) value based on number range

I have a number, e.g. $humidity
I need to check value of this number and get a fixed value if number is in a predetermined range.
E.g.
if ($humidity<30) {
return 'dry';
}
if ($humidity>=30 && $humidity <=40) {
return 'wet'
}
if ($humidty>40 && $humidity<70) {
return 'confortable'
}
And so on.
Is there another possibility to don't use 4/5 different if ?
As long as you process the values in order, you don't need both the upper and lower values of each range. Then you can utilize short-circuiting and just put everything in a loop:
function nameOf($humidity)
{
$list = [
30 => 'dry',
40 => 'wet',
70 => 'comfortable',
];
foreach ($list as $value => $name) {
if ($humidity < $value) {
return $name;
}
}
return 'default';
}
I think switch is great for this usage:
$result = null;
switch(true) {
case $humidity < 30:
$result = 'dry';
break;
case $humidity >= 30 && $humidity < 40:
$result = 'wet';
break;
case $humidty > 40 && $humidity < 70:
$result = 'comfortable';
break;
}
return $result;
you can create an range array for your temps and then array_walk that to find the right range.
$h=40;
$harr = [ 0 => 'dry', 1 => 'wet', 2 => 'confy'];
$range = array_flip(range(0, 100, 30));
array_walk($range, 'findHumidity', $range);
var_dump($harr[$result]);
function findHumidity($value, $key, $r){
global $h; //if you using a class and properties you can ignore these two lines
global $result;
$normalRange = array_flip($r);
if($h> $key && $normalRange[$value+1]!=null && $h<= $normalRange[$value+1]){
$result = $value;
}
}
Working example: https://3v4l.org/fWLDu (a simplified version: https://3v4l.org/ROjWk)
or you can define the range array manually like here: https://3v4l.org/B0oTU

How to find backward primes within a range of integers?

I'm trying to solve a backward prime question.
Following is the question:
Find all Backwards Read Primes between two positive given numbers (both inclusive), the second one being greater than the first one. The resulting array or the resulting string will be ordered following the natural order of the prime numbers.
Example
backwardsPrime(2, 100) => [13, 17, 31, 37, 71, 73, 79, 97]
backwardsPrime(9900, 10000) => [9923, 9931, 9941, 9967]
I tried doing something like this:
public function backwardPrime()
{
$start = 7000;
$stop = 7100;
$ans = [];
while($start <= $stop)
{
if($start > 10)
{
if($start !== $this->reverse($start))
{
if($this->isPrime($start) && $this->isPrime($this->reverse($start)))
{
array_push($ans, $start);
}
}
}
$start++;
}
return $ans;
}
public function reverse($num)
{
$reverse = 0;
while($num > 0)
{
$reverse = $reverse * 10;
$reverse = $reverse + $num%10;
$num = (int)($num/10);
}
return $reverse;
}
public function isPrime($num)
{
if($num == 1 || $num == 2 || $num == 3)
return true;
elseif ($num%2 == 0 || $num%3 == 0)
return false;
else
{
$i=5;
while($i<=$num/2)
{
if($num%$i===0)
{
return false;
}
$i++;
}
}
return true;
}
I'm able to get the appropriate answer but while doing the same in single function I'm not able to get it:
public function backwardPrimes()
{
$start = 7000;
$stop = 7100;
$ans = [];
while($start <= $stop)
{
$isStartPrime = true;
$isReversePrime = true;
if($start > 10)
{
$reverse = 0;
$num = $start;
while($num > 0)
{
$reverse = $reverse * 10;
$reverse = $reverse + $num%10;
$num = (int)($num/10);
}
if($start !== $reverse)
{
if($start%2 != 0 && $start%3 != 0)
{
$i =5;
while($i<=$start/2)
{
if($start%$i === 0)
{
$isStartPrime = false;
break;
}
$i++;
}
}
if($reverse%2 != 0 && $reverse%3 != 0)
{
$i =5;
while($i<=$reverse/2)
{
if($reverse%$i === 0)
{
$isReversePrime = false;
break;
}
$i++;
}
}
if($isStartPrime && $isReversePrime)
{
array_push($ans, $start);
}
}
}
$start++;
}
return $ans;
}
I don't know where I'm having mistake, guide me.
Thanks.
An emirp ("prime" spelled backwards) is a prime whose (base 10) reversal is also prime, but which is not a palindromic prime. in other words
Backwards Read Primes are primes that when read backwards in base 10 (from right to left) are a different prime. (This rules out primes which are palindromes.)
try this short solution in which I use two helper function reverse and isPrime :
isPrime: Thanks to #Jeff Clayton for his method to test prime numbers, for more information click the link below https://stackoverflow.com/a/24769490/4369087
reverse: that use the php function [strrev()][1], this method take a string a reverse it, we'll use this trick to reverse a number by converting it to a string reverse it and converting back to an integer.
backwardsPrime: this last function's job is itterating over a range of numbers from $min value to $max value and test if the number if a prime number and it's reverse is a prime number as well and it's not a palindrome number if all of those conditions are true then we addit to the result array.
implementation
function isPrime($number)
{
return !preg_match('/^1?$|^(11+?)\1+$/x', str_repeat('1', $number));
}
function reverse($n)
{
return (int) strrev((string) $n);
}
function backwardsPrime($min, $max)
{
$result = [];
foreach(range($min, $max) as $number) {
$reverse = reverse($number);
if($reverse !== $number && isPrime($number) && isPrime($reverse)) {
$result[] = $number;
}
}
return $result;
}
echo "<pre>";
print_r(backwardsPrime(2, 100));
print_r(backwardsPrime(9900, 10000));
output :
Array
(
[0] => 13
[1] => 17
[2] => 31
[3] => 37
[4] => 71
[5] => 73
[6] => 79
[7] => 97
)
Array
(
[0] => 9923
[1] => 9931
[2] => 9941
[3] => 9967
)
you can even optimize the backwardsPrime function like this :
function backwardsPrime($min, $max)
{
$result = [];
foreach(range($min, $max) as $number) {
$reverse = reverse($number);
if($reverse !== $number && !in_array($number, $result) && isPrime($number) && isPrime($reverse)) {
$result[] = $number;
}
}
return $result;
}

Can I use $++ PHP in a foreach loop to increment the 'as' index?

If I wanted to create a foreach loop that would add every second player to team A or team B
foreach ( $selectedplayers as $selectedplayer ) {
TeamA[] = $selectedplayer;
$selectedplayer++;
TeamB[] = $selectedplayer;
}
I know I can do it with a for loop but was interested to see if it could be done this way.
No. $selectedplayer is the actual value of each element. Either use your own counter:
$i = 0;
foreach ( $selectedplayers as $selectedplayer ) {
if($i % 2 === 0)) {
$TeamA[] = $selectedplayer;
} else {
$TeamB[] = $selectedplayer;
}
$i++;
}
Or use the $key of the array if the keys are sequential (or at least odd, even pattern):
foreach ( $selectedplayers as $key => $selectedplayer ) {
if($key % 2 === 0)) {
$TeamA[] = $selectedplayer;
} else {
$TeamB[] = $selectedplayer;
}
}

PHP: Sort array according to another array of different length

I have two arrays of different length:
$paths_table = array("TS-0007_a.jpg", "TS-0040_a.JPG", "TS-0040_b.JPG", "TS-0040_f.JPG", "TS-0041_a.JPG", "TS-0041_b.JPG");
$order_table = array("TS-0040","TS-0007","TS-0041");
and I want to sort the first one using the second so that the output will be the array
$final_table = array("TS-0040_a.JPG", "TS-0040_b.JPG", "TS-0040_f.JPG", "TS-0007_a.jpg", TS-0041_a.JPG", "TS-0041_b.JPG")
Assuming that I'm going to use
strpos($paths_table[$i], $order_table[$j]);
to check if the string of $order_table is included in any of the $paths_table.
How can I accomplish this?
Preprocess the array so that each item contains an index of its prefix (that is, turn 'TS-0007_a.jpg' into [1,'TS-0007_a.jpg']):
foreach($paths_table as &$v) {
foreach($order_table as $n => $o)
if(strpos($v, $o) === 0) {
$v = [$n, $v];
break;
}
}
sort the array:
sort($paths_table);
and remove indexes:
foreach($paths_table as &$v)
$v = $v[1];
The following piece of code can off course be optimized in several ways, but for the sake of clarity I didnt.
$paths_table = array("TS-0007_a.jpg", "TS-0040_a.JPG", "TS-0040_b.JPG", "TS-0040_f.JPG", "TS-0041_a.JPG", "TS-0041_b.JPG");
$order_table = array("TS-0040","TS-0007","TS-0041");
$sorter = new PrefixSorter($order_table);
$output = usort($paths_table, array($sorter, 'sort'));
var_dump($paths_table);
class PrefixSorter {
private $prefixes;
function __construct($prefixes) {
$this->prefixes = $prefixes;
}
function sort($path1, $path2) {
$prefix1 = -1;
$prefix2 = -1;
foreach($this->prefixes as $index=>$prefix) {
if (substr($path1, 0, strlen($prefix)) == $prefix) $prefix1 = $index;
if (substr($path2, 0, strlen($prefix)) == $prefix) $prefix2 = $index;
}
if (($prefix1 == -1 && $prefix2 == -1) || $prefix1 == $prefix2) {
return 0;
}
else if ($prefix1 == -1 || $prefix1 > $prefix2) {
return 1;
}
else if ($prefix2 == -1 || $prefix1 < $prefix2) {
return -1;
}
}
}
I made a few assumptions:
You want to sort on the prefixes given in order_table
Prefixes not given are put at the back unordered.
You can off course change the code to match on string containment instead of prefixing

PHP Array Zeros except for one, extract that value

I have an array with values that change from time to time. It will usually look like this:
Array ( [0] => 0 [1] => 0 [2] => 9876 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 )
All of the Values will be 0 except for one (index location will change).
If more than one value is greater than 0, I need to execute a specific command.
Else, if only one value is greater than 0, I need to take that value and pass it to a specific command.
Create a new array containing only the non-null values. array_filter without callback will return all elements which do not evaluate to FALSE.:
$a = array(...);
$values = array_filter($a);
switch(count($values)) {
case 0: echo 'All 0!'; break;
case 1: specificCommandWithValue($values[0]); break;
default: executeSpecificCommand(); break;
}
If you have false-y values, you want to keep (FALSE, NULL, '0', ''), pass a callback which does strict value comparison: function($el) { return $el !== 0; }
try
$count =0;
foreach($array as $item){
if($item !=0){
$count = $count+1;
}
}
if($count > 1){
//execute a specific command
}elseif($count == 1){
// take that value and pass it to a specific command
}else{
//all value are zero
}
Try this code.
<?PHP
$array = array(
"1" => "0",
"2" => "0",
"3" => "0",
"4" => "24",
"5" => "0");
$zero_plus_keys = 0;
$zero_plus_val = array();
foreach($array as $key => $val)
{
if($val > 0)
{
$zero_plus_keys++;
$zero_plus_val = array($key,$val);
}
}
if($zero_plus_keys == 1)
{
echo "In array '".$zero_plus_val[0]."' key contains Greater than zero value. the value is = '".$zero_plus_val[1]."'";
}
elseif($zero_plus_keys > 1)
{
echo "More keys Greater than zero(0)";
}
else
{
echo "All keys contains zero(0)s only...";
}
?>
This is #NullPointer's answer, but I added in a variable to hold "that value".
$count =0;
$nonzero = null;
foreach($array as $item){
if($item !=0){
$count = $count+1;
$nonzero = $item;
}
}
if($count > 1){
//execute a specific command
}elseif($count == 1){
specific_command($nonzero);
}else{
//all value are zero
}
$array = 'your array';
function Find($array){
$count = 0;
$needle = -1;
foreach($array as $item){
if($item > 0){
$count++;
$needle = $item;
}
if($count > 1)
return -1; //error as number of non-zeroes greater than 1
}
if($count > 0)
return $needle; //returns the required single non-zero item
return 0; // returns zero if nothing is found
}
$return = Find($array);
Just for fun :P
$array = array_flip(array_flip($array));
sort($array);
if (count($array) > 2) moreThanOne();
else onlyOne($array[1]);
Filter the array and if there is only one use current() to get it:
if(count($n = array_filter($array)) == 1) {
execute_command(current($n));
} else {
execute_command();
}

Categories