I'm trying to get a count of how many instances of 'UnitSqFeet' are within a certain range.
For example how many instances are between 0 - 175, or 176 - 300.
Here is a part example of the array (it contains 46 in total).
Array (
[0] => Array ( [UnitNumber] => 1.03 [UnitSqFeet] => 60.75 )
[1] => Array ( [UnitNumber] => 1.04 [UnitSqFeet] => 160.39 )
[2] => Array ( [UnitNumber] => 1.05 [UnitSqFeet] => 231.55 )
[3] => Array ( [UnitNumber] => 1.06 [UnitSqFeet] => 280.24 )
)
The 'UnitSqFeet' is a string so I'm assuming it'll have to be converted somewhere in there.
I managed to get this working as below but it would only output this in the first cell of an html table and not the rest. After doing some research on here I understand it's because I was repeating the query for every cell and after the first it had already retrieved all the rows. Not sure if there's a solution whereby I can just reset the query?
$counter = 0;
while ($units = odbc_fetch_array($result)) {
$num = $units['UnitSqFeet'];
$float = (float)$num;
if ($float > 0 && $float < 175) {
count($float);
$counter++;
};
};
echo $counter;
Try as
$count = $count2 = 0;
$result = array();
foreach(array_column($arr,'UnitSqFeet') as $key => $value){
if(round($value) <= 170){
if(!isset($result['0-170'])) $result['0-170'] = $count++;
$result['0-170'] = $count++;
}else{
if(!isset($result['171-300'])) $result['171-300'] = $count2++;
$result['171-300'] = $count2++;
}
}
print_r($result);
Fiddle
Try it here: http://viper-7.com/ofozYB
<?php
$UnitSqFeet = array_column($array, 'UnitSqFeet');
$output = array_filter($UnitSqFeet, function ($var) {
return ( $var >= 70 && $var <= 250 ); // return if between 70 & 250
});
print_r($output);
echo "There is " . count($output) . " values in the selected range";
Related
Im trying to parse an array in PHP to fill Null values as averages between the closest two values before inserting into MySQL (5). I have currently tried the below code but the code moves the pointer for the entire array when I call next($value['wind']).
f_data:
(
[2019-05-06 09:00:00] => Array
(
[temp] => 50
[wind] => 10
[rain] => 50
)
[2019-05-06 10:00:00] => Array
(
[temp] => 65
)
[2019-05-06 11:00:00] => Array
(
[temp] => 70
[wind] => 8
[rain] => 30
)
)
code thus far:
foreach ($f_data as $key => $value) {
$f_weather_date = $key;
$future_weather = $value['temp'];
if ($value['wind'] > 0) {
$f_wind = $value['wind'];
$f_wind_last = $f_wind;
} else {
$f_wind = $f_wind_last;
$x = 0;
$f_wind_next = 0;
for ($x = 0; $f_wind_next = 0; $x++) {
$f_wind_next = next($value['wind']);
return $x;
}
$f_wind = ($f_wind_next + $f_wind_last) / $x;
}
if ($value['rain'] > 0) {
$f_rain = $value['rain'];
$f_rain_last = $f_rain;
} else {
$f_rain = $f_rain_last;
}
$sql = "INSERT INTO `Table` (`Date`, `Temp`, `Wind`, `Rain`) VALUES ('$f_weather_date', $future_weather, $f_wind, $f_rain)";
mysqli_query($conn, $sql);
}
Expected results would return [wind] => 9 and [rain] => 40 at 10:00:000
First extract the keys and then iterate with int key. This way you can calculate the average easily.
Consider:
$keys = array_keys($f_data);
$arr = array_values($f_data);
foreach($arr as $k => &$v) {
if (!isset($v['rain']) { //you can do the same for wind
$v = ($arr[$k -1]['rain'] + $arr[$k +1]['rain']) /2;
}
}
Now return the original keys with:
$f_data = array_combine($keys, $arr);
Notice you need to take care of case the last eleme t is missing data and when 2 neighbors elements missing data
I'm trying to fill an array with a for loop. This is done to get the amount of pages a certain book has. but when executing the code, it skips the first object in the array. Can anyone tell me why? (I thought it was because $i starts at 1 instead of 0 but that doesn't seem to change anything)
if(!empty($article['finishing'])){
$numPages = $article['copies'];
$arrayIndexNumber = [];
for($i=1; $i <= $numPages; $i++){
$arrayIndexNumber[] = $i;
}
if(count($arrayIndexNumber) >= 1 ){
if(count($arrayIndexNumber) == 1){
$output['attributes']['EFPageRange'] = 1;
$print_jobs[$article['id']][] = $output;
}
if(count($arrayIndexNumber) > 1){
$comma_separated1 = implode(", ", ['1', $article['copies']]);
$output['attributes']['EFPageRange'] = $comma_separated1;
$print_jobs[$article['id']][] = $output;
}
array_shift($arrayIndexNumber);
array_pop($arrayIndexNumber);
$comma_separated2 = implode(", ", $arrayIndexNumber);
$output['attributes']['EFPageRange'] = $comma_separated2;
if(count($arrayIndexNumber) >= 2){
$print_jobs[$article['id']][] = $output;
}
}
$article['file_url'] = 'i has finishing';
$output['attributes']['username'] = $article['file_url'];
}
above code outputs:
[0] => Array
(
[attributes] => Array
(
[title] => 277569
[EFPrintSize] => a4
[num copies] => 1
[num pages] => 119
[EFPCName] => 80
[EFDuplex] => TopTop
[EFPageRange] => 1, 119
)
)
instead of:
[0] => Array
(
[attributes] => Array
(
[title] => 277564
[EFPrintSize] => a4
[num copies] => 1
[num pages] => 45
[EFPCName] => 80
[EFDuplex] => false
[EFPageRange] => 1, 45
[username] => i has finishing
[EFColorMode] => Grayscale
)
)
Your first array element is deleted because of array_shift:
array_shift($arrayIndexNumber);
array_shift
array_shift — Shift an element off
the beginning of array
Debug your code:
for($i=1; $i <= $numPages; $i++){
$arrayIndexNumber[] = $i;
}
echo '<pre>';
print_r($arrayIndexNumber); // Check what the array returns
php array indexes starts to count from zero
for($i=1; $i <= $numPages; $i++)
^^^
change it to $i=0
I have a form like this
for($x=0;$x<8;$x++){
echo "<input name='test[]'> <br>";
}
then i'm input, and i get this result
Array
(
[0] => 1
[1] => 2
[2] => 0
[3] => 0
[4] => 0
[5] =>
[6] => 0
[7] =>
)
I want to sum then divide it. I'm using array_sum to sum it. Now the problem, how can i divide it depends on my input(s) not the many of input field(s) and i don't want count the zero as input.
so it will become something like this
(1+2) / 2
how can i achieve that? sorry for my bad english.
If you want only non-zero values to be counted, you'll need to filter those zero and empty values away. You can still use array_sum(), to sum the array, but you'll need a filter on your count(), because count() will count all elements, regardless of the content (except null-values).
array_filter() takes care of this. So you'll simply need this
echo array_sum($myArray) / count(array_filter($myArray)); // Outputs 1.5 with your sample-array
Live demo
http://php.net/count
http://php.net/array_filter
Try This:
$myArray = Array
(
[0] => 1
[1] => 2
[2] => 0
[3] => 0
[4] => 0
[5] =>
[6] => 0
[7] =>
)
$countArray = 0;
foreach($myArray as $v){
if($v !== 0 || $v !== ''){ //skip if 0 or empty
$countArray++
}
}
$result = array_sum($myArray) / $countArray;
You may use this PHP custom Function
<?php
/* Function for check and remove blank and empty element from array */
function remove_blank($arra)
{
$newArr = array();
$total = count($arra);
for($i = 0; $i <= $total; $i++)
{
if($arra[$i] != "" && $arra[$i] >= 0)
{
$newArr[] = $arra[$i];
}
}
echo "<pre>";
print_r($newArr);
}
$arra = array(1, 2, 0, 0,'', 0,'');
remove_blank($arra);
?>
I have two arrays, Array 1 being a donor array which has a series of values (not necessarily equal values as in my example). Array 2 is the desired result and would store a series of sub arrays with values from Array 1 where each sub array's total sum would not exceed 25. If it does, the excess would get pushed to the next index in Array 2 where the rules would also apply.
Donor array (Array 1):
$a1=array(10,10,10,10,10,10,10,10,10,10);
Desired output (Array 2):
Array
(
[0] => 10,10,5
[1] => 5,10,10
[2] => 10,10,5
[3] => 5,10,10
)
Here the code I tried but it gets an error:
Notice: Undefined offset: 10...etc.
$a1=array(10,10,10,10,10,10,10,10,10,10);
$a2=array();
$count=count($a1);
for($i=0;$i<$count;$i++){
$a2count=array_sum($a2);
if($a2count>25){
$i=$i+1;
$a2[$i]=$a1[$i];
}
else{
$a2[$i]=$a1[$i];
}
}
print_r($a2);
I don't know what logic to implement and get result I'm looking for.
Mayhaps something like this will work for you. I will notate so it's not just a copy-and-paste answer. Maybe someone will get some insight on it to improve it in the future:
function slitArray($a1,$num = 25)
{
# Used to store the difference when the value exceeds the max
$store = 0;
# Storage container that will be built using sums/diffs
$new = array();
# Loop through the main array
foreach($a1 as $value) {
# If the last key/value pair in our return array is an array
if(is_array(end($new)))
# Add up the values in that array
$sum = array_sum(current($new));
else
# If not array, no values have been stored yet
$sum = 0;
# This just gets the last key
$count = (count($new)-1);
# Assign last key
$i = ($count <= 0)? 0 : $count;
# If the sum of the current storage array plus the value
# of the current array is greater than our max value
if(($sum + $value) > $num) {
# Take max and remove the current total of storage array
$use = ($num-$sum);
# Take what's left and remove it from the current value
$store = ($value-$use);
# If the current stored value (the value we want to push to
# the next storage k/v pair) is more than the max allowed
if($store > $num) {
# Takes a value, checks if it's greater than max,
# and if it is, breaks the value up by max as a divider
$divide = function($store,$num)
{
if($store > $num) {
$count = ceil($store/$num);
for($i=0; $i<$count; $i++) {
$new[] = ($store > $num)? $num : $store;
$store -= $num;
}
return $new;
}
else
return array($store);
};
# This should either be an array with 1 or more values
$forward = $divide($store,$num);
# Do a look forward and add this excess array into our
# current storage array
$a = $i;
foreach($forward as $aVal) {
$new[$a+=1][] = $aVal;
}
}
# If the store value is less than our max value, just add
# it to the next key in this storage array
else {
$new[$i+1][] = $store;
# Reset the storage back to 0, just incase
$store = 0;
}
}
# Set the current "use" value as the current value in our
# from-array. Since it doesn't exceed the max, it just gets
# added to the storage array
else
$use = $value;
# Sometimes the math makes $use 0, keep that out of the
# storage array. The $use value is the current value to add at
# the time of iteration. Previous storage values are added as
# future-keys
if($use > 0)
$new[$i][] = $use;
}
# Return the final assembled array
return $new;
}
# To use, add array into function
$a1 = array(10,10,10,10,10,10,10,10,10,10);
# to split using different max value, just add it to second arg
# example: slitArray($a1,20);
print_r(slitArray($a1));
Gives you:
Array
(
[0] => Array
(
[0] => 10
[1] => 10
[2] => 5
)
[1] => Array
(
[0] => 5
[1] => 10
[2] => 10
)
[2] => Array
(
[0] => 10
[1] => 10
[2] => 5
)
[3] => Array
(
[0] => 5
[1] => 10
[2] => 10
)
)
An array input of:
$a1 = array(23,2,71,23,50,2,3,4,1,2,50,75);
Gives you:
Array
(
[0] => Array
(
[0] => 23
[1] => 2
)
[1] => Array
(
[0] => 25
)
[2] => Array
(
[0] => 25
)
[3] => Array
(
[0] => 21
[1] => 4
)
[4] => Array
(
[0] => 19
[1] => 6
)
[5] => Array
(
[0] => 25
)
[6] => Array
(
[0] => 19
[1] => 2
[2] => 3
[3] => 1
)
[7] => Array
(
[0] => 3
[1] => 1
[2] => 2
[3] => 19
)
[8] => Array
(
[0] => 25
)
[9] => Array
(
[0] => 6
[1] => 19
)
[10] => Array
(
[0] => 25
)
[11] => Array
(
[0] => 25
)
[12] => Array
(
[0] => 6
)
)
Here you go: The logic is not so hard. Hope it helps.
<?php
$a1=array(10,10,10,10,10,10,10,10,10,10);
$a2 = [];
$a3 = [];
$m = 0;
for($i = 0; $i < count($a1); ++$i){
$m += $a1[$i];
if($m > 25){
$n = $m % 25;
if(array_sum($a2) != 25){
$a2[] = $n;
}
$a3[] = implode(',', $a2);
$a2 = [];
$m = $n;
$a2[] = $n;
} else{
$a2[] = $a1[$i];
}
}
$a3[] = implode(',', $a2);
print_r($a3);
?>
The final code for this question
<?php
function slitArray($a1,$num = 25)
{
$store = 0;
$new = array();
foreach($a1 as $value) {
if(is_array(end($new)))
$sum = array_sum(current($new));
else
$sum = 0;
$count = (count($new)-1);
$i = ($count <= 0)? 0 : $count;
if(($sum + $value) > $num) {
$use = ($num-$sum);
$store = ($value-$use);
if($store > $num) {
$divide = function($store,$num)
{
if($store > $num) {
$count = ceil($store/$num);
for($i=0; $i<$count; $i++) {
$new[] = ($store > $num)? $num : $store;
$store -= $num;
}
return $new;
}
else
return array($store);
};
$forward = $divide($store,$num);
$a = $i;
foreach($forward as $aVal) {
$new[$a+=1][] = $aVal;
}
}
else {
$new[$i+1][] = $store;
$store = 0;
}
}
else
$use = $value;
if($use > 0)
$new[$i][] = $use;
}
return $new;
}
$a1 = array(10,20,30,40,50,60);
$arr=slitArray($a1);
print_r($arr);
?>
Let me help you a bit using Pseudocode:
ar1 = {10,10,10,20,40,[0]=>1,[0]=>3,[0]=>4};
ar2 = new array (ar.length) \\ worst case
int c = 0; \\current
foreach (ar1 as $value){
ar2 [c]+=ar1[i];
if (ar2 [c]>25){ c++;}
}
Logic behind the code:
Add the value of ar1[i] to the current ar2 value till it passes your limit (25 in this case). If it exceeds you boundary, than move to the next value in the goal array. The worst case would be, that every value is over 25, so it would be an exact copy of the original array.
Here the php code:
$ar1=array(10,10,10,10,10,10,10,10,10,10);
$ar2 = array(0,0,0,0,0,0,0,0,0,0);
$c = 0;
foreach( $ar1 as $key => $value ){
$ar2[$c]=$value+$ar2[$c];
if ($ar2[$c]>25){$c++;}
}
I have looked and googled many times I found a few posts that are simular but I can not find the answer Im looking for so I hope you good people can help me.
I have a function that returns a simple number array. The array number values are dynamic and will change most frequently.
e.g.
array(12,19,23)
What I would like to do is take each number value in the array, compare it to a set range and return all the lower value numbers up to and including the value number in the array.
So if I do this:
$array = range(
(11,15),
(16,21),
(22,26)
);
The Desired output would be:
array(11,12,16,17,18,19,22,23)
But instead I get back all the numbers in all the ranges.
array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26)
What would be a simple solution to resolve this?
Try this code
$range = array(
array(11,15),
array(16,21),
array(22,26),
);
$array = array(12,19,23);
$result = array();
foreach($range as $key=>$value)
{
//$range1 =$range[$key];
$min = $range[$key][0];
$max = $range[$key][1];
for($i = $min;$i<=$max;$i++)
{
if($i <= $array[$key])
{
array_push($result,$i);
}
}
}
echo "<pre>";print_r($result);
Iterate over each element, find the the start and end values you need to include, and append them to the output array:
$a = array(12,19,23);
$b = array(
range(11,15),
range(16,21),
range(22,26)
);
$c = array();
foreach ($a as $k => $cap) {
$start = $b[$k][0];
$finish = min($b[$k][count($b[$k])-1], $cap);
for ($i = $start; $i <= $finish; $i++) {
$c[] = $i;
}
}
print_r($c);
prints
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)
My solution is probably not the most efficient, but here goes:
$numbers = array(12,19,23);
$ranges = array(
array(11,15),
array(16,21),
array(22,26)
);
$output = array();
// Loop through each of the numbers and ranges:
foreach($numbers as $num) {
foreach($ranges as $r) {
if ($num >= $r[0] && $num <= $r[1]) {
// This is the correct range
// Array merge to append elements
$output = array_merge($output, range($r[0], $num));
break;
}
}
}
// Sort the numbers if you wish
sort($output, \SORT_NUMERIC);
print_r($output);
Produces:
Array
(
[0] => 11
[1] => 12
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 22
[7] => 23
)