taking value to next 10th position - php

I know that it's rather an unusual question, but I need to find next 10th position
Let me clear with an example:
I will have some numbers like
2, 5, 8, 13, 28, 35, 42, 49
I want to find the next 10th position
2 => 10
5 => 10
8 => 10
13 => 20
28 => 30
35 => 40
42 => 50
49 => 50
How can I find it, please help guys. thanks.
Sorry for poor english.

Divide by 10, ceil(), multiply by 10.

$next10th = round( $number + 5 , -1 , PHP_ROUND_HALF_DOWN )
If the number is integer, you can also use:
$next10th = $number + 9 - (($number-1) % 10)

$numbers = array(2, 5, 8, 13, 28, 35, 42, 49);
foreach ($numbers as $n) {
echo $n . ' => ' . ((floor($n / 10) + 1) * 10) . "<br />\n";
}
or
$numbers = array(2, 5, 8, 13, 28, 35, 42, 49);
foreach ($numbers as $n) {
echo $n . ' => ' . (ceil($n / 10) * 10) . "<br />\n";
}
Both output:
2 => 10
5 => 10
8 => 10
13 => 20
28 => 30
35 => 40
42 => 50
49 => 50

Related

Calculating Median of an array in PHP

I'm trying to figure out how to calculate the median of an array of randomly generated numbers. I have the array all set up, but I'm having trouble putting together a function for the calcuation.
This is what I have so far:
//array
$lessFifty = array();
$moreFifty = array();
//number generation
for ($i = 0; $i<=30; $i++) {
$number = rand(0, 100);
//Sorting <50>
if ($number < 50 ) {
$lessFifty[] = $number;
} else {
$moreFifty[] = $number;
}
}
echo print_r($lessFifty);
echo "<br>" ;
echo print_r($moreFifty);
//Average
echo "<p> Average of values less than fifty: </p>";
print array_sum($lessFifty) / count($lessFifty) ;
echo "<p> Average of values greater than fifty: </p>" ;
print array_sum($moreFifty) / count($moreFifty) ;
//Median
$func = function (median ($array, $output = $median)){
if(!is_array($array)){
return FALSE;
}else{
switch($output){
rsort($array);
$middle = round(count($array) 2);
$total = $array[$middle-1];
break;
return $total;
}
}
echo $func ;
I'm pretty sure that I'm doing this median section completely wrong. I'm just learning and its proving to be a challenge.
Be careful about how you write your for() loop. If you want 30 entries, then you should not use <= or you will end up with 31 because $i starts with 0.
Build an array of the random numbers, then sort them.
Then determine if you have a central entry (odd array length) or if you need to average the middle two entries (even array length).
Here is a modern implementation of a median method posted in 2022 on CodeReview.
Code: (Demo)
$limit = 30; // how many random numbers do you want? 30 or 31?
for ($i = 0; $i < $limit; ++$i) {
$numbers[] = rand(0, 100);
}
var_export($numbers);
//echo "\n---\nAverage: " , array_sum($numbers) / $limit;
echo "\n---\n";
sort($numbers);
$count = sizeof($numbers); // cache the count
$index = floor($count/2); // cache the index
if (!$count) {
echo "no values";
} elseif ($count & 1) { // count is odd
echo $numbers[$index];
} else { // count is even
echo ($numbers[$index-1] + $numbers[$index]) / 2;
}
Possible Output:
array (
0 => 27,
1 => 24,
2 => 84,
3 => 43,
4 => 8,
5 => 51,
6 => 60,
7 => 86,
8 => 9,
9 => 48,
10 => 67,
11 => 20,
12 => 44,
13 => 85,
14 => 6,
15 => 63,
16 => 41,
17 => 32,
18 => 64,
19 => 73,
20 => 43,
21 => 24,
22 => 15,
23 => 19,
24 => 9,
25 => 93,
26 => 88,
27 => 77,
28 => 11,
29 => 54,
)
---
43.5
After sorting, elements [14] and [15] hold 43 and 44 respectively. The average of these "middle two" values is how the result is determined. (Hardcoded numbers demo)
If you want a short, inflexible, hardcoded snippet, then you can use 30 and 14 and 15 as your predetermined size and indexes.
for ($i = 0; $i < 30; ++$i) {
$numbers[] = rand(0, 100);
}
sort($numbers);
echo ($numbers[14] + $numbers[15]) / 2;

How to group of 2 arrays

I have 2 arrays like this
$head = array(7, 1, 1, 1, 1, 14, 14, 14, 9, 9, 9, 13, 13, 13, 3, 3, 5, 8, 8, 8, 2, 2); //count =22
$customer = array(1, 7, 9, 13, 14, 1, 9, 13, 1, 13, 14, 1, 9, 14, 2, 8, 8, 2, 3, 5, 3, 8); //count=22
And I want to group this 2 arrays by consider at $customer, if $customer[0]=1 in $customer[1-21] and $head[1-21] will not have a value 1, such as in the $head[1] have a value 1, So delete at $head[1] and $customer[1]. And then consider at $customer[6]. The value is 9. It means in $head[7-21] and $customer[7-21] will not have a value 9.
I am trying to write a code for this concept like this. Here is my code
for ($i = 0; $i < count($head); $i++) {
for ($j = $i + 1; $j < count($customer); $j++) {
if ($customer[$i] == $head[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
if ($customer[$i] == $customer[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
}
}
print_r($head);
print_r($customer);
the result is of $head and $customer is:
Array ( [0] => 7 [6] => 14 [7] => 14 [13] => 13 [14] => 3 [15] => 3 [16] => 5 [17] => 8 [18] => 8 [19] => 8 [20] => 2 [21] => 2 )
Array ( [0] => 1 [6] => 9 [7] => 13 [13] => 14 [14] => 2 [15] => 8 [16] => 8 [17] => 2 [18] => 3 [19] => 5 [20] => 3 [21] => 8 )
I found that it's wrong. Because the real result should be:
Array ( [0] => 7 [6] => 14 [7] => 14 [14] => 3 [15] => 3 )
Array ( [0] => 1 [6] => 9 [7] => 13 [14] => 2 [15] => 8 )
Please help me to fix this problem.
Your logic is all ok but when you unset a particular index then all other index after when you iterate it again then i index is missing. Just open the error and warning then you will see
Notice: Undefined offset
I have just replaced your uset to assign it to ''. So you can understand it
<?php
$head = array(7, 1, 1, 1, 1, 14, 14, 14, 9, 9, 9, 13, 13, 13, 3, 3, 5, 8, 8, 8, 2, 2); //count =22
$customer = array(1, 7, 9, 13, 14, 1, 9, 13, 1, 13, 14, 1, 9, 14, 2, 8, 8, 2, 3, 5, 3, 8); //count=22
for ($i = 0; $i < count($head); $i++) {
for ($j = $i + 1; $j < count($customer); $j++) {
if ($customer[$i] == $head[$j]) {
$head[$j] = '';
$customer[$j] = '';
}
if ($customer[$i] == $customer[$j]) {
$head[$j] = '';
$customer[$j] = '';
}
}
}
print_r(array_diff($head, [''])); // remove all the '' entries
print_r(array_diff($customer, [''])); // remove all the '' entries
The problem is that count() only returns a count of the set elements in the array. So if you unset them it will be reduced and you won't arrive at the end of the array. To fix, calculate the count at the start and store in a variable:
$headcount = count($head);
$customercount = count($customer);
for ($i = 0; $i < $headcount; $i++) {
for ($j = $i + 1; $j < $customercount; $j++) {
if ($customer[$i] == $head[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
if ($customer[$i] == $customer[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
}
}

How to right align numbers from an array in php?

I have a problem with aligning numbers from a multidimensional array. I want to print the following result:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
And I want all of the numbers to be aligned with the second digit of the next rows. However my result is that:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
I did this in C# by using:
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write("{0,4}", matrix[row, col]);
}
But how can I receive this result in PHP?
You can use str_pad
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
$test = '';
foreach ($arr as $key => $value) {
if ($key % 4 == 0) {
$test .= "\n";
}
$test .= str_pad($value, 4, ' ', STR_PAD_LEFT);
}
echo "<pre>$test</pre>";
The result would be:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Adding values together based on their position in PHP

Each number has a corresponding value with it. There are many numbers which I can demonstate in a table here with their appropriate values:
[N] [V] N=Number V=Value
2 19
4 19
6 19
8 21
10 21
12 22
14 23
16 23
18 23
20 33
22 37
24 42
26 45
28 48
30 50
32 55
34 61
36 66
38 72
40 78
42 155
44 179
46 202
48 233
50 360
There is a process that a user will go through where they go from Number x to Number y. The values inbetween those numbers need to get added together. So for example, let's say a user goes from 16 to 38:
[N] [V] N=Number V=Value
2 19
4 19
6 19
8 21
10 21
12 22
14 23
[16][23]--
18 23 |
20 33 |
22 37 |
24 42 |
26 45 |
28 48 |---- All of these values get added together
30 50 |
32 55 |
34 61 |
36 66 |
[38][72]--
40 78
42 155
44 179
46 202
48 233
50 360
So the users total value would equal be:
23 + 23 + 33 + 37 + 42 + 45 + 48 + 50 + 55 + 61 + 66 + 72
Total Value = 555
The problem is, is that I have no idea how I to put this together in code. Like how to assign these values to their specific number and how to add those specific values together to get me a result. In PHP I simply do not know where to begin with this.
Also, the approximate values from the numbers can be represented by this equation:
v = 11.218e^(0.057n)
I would imagine this would be useful in making this whole process easier but I am still not sure how to go about implementing all of this. Any help would be very much apprieciated!
Put each each number with it's corresponding value into an array making number as key and value pair like this.
<?php
$arr = array(
2=> 19,
4=> 19,
6=> 19,
8=> 21,
10=> 21,
12=> 22,
14=> 23,
16=> 23,
18=> 23,
20=> 33,
22=> 37,
24=> 42,
26=> 45,
28=> 48,
30=> 50,
32=> 55,
34=> 61,
36=> 66,
38=> 72,
40=> 78,
42=> 155,
44=> 179,
46=> 202,
48=> 233,
50=> 360,
);
?>
Loop array with foreach loop like this
<?php
$sum = 0;
foreach($arr as $k => $v) {
if($k >= 16 && $k <= 38)
$sum += $v;
}
?>
There is another way using for loop statement, put both number in two separate array ($n and $v). Iterate the loop of the first array($n) and find the value from second array($v) through the index number of first array. But both array count should have same.
Example-
<?php
$n = array(2,4,6,8,10,12,14,16,18,20);
$v = array(19,19,19,21,21,22,23,23,23,33);
$sum = 0;
for($i=0, $i<count($n); $i++) {
if($n[$i] >= 16 && $n[$i] <= 38)
$sum += $v[$i];
}
?>
You would put your number and value pairs into an key / value array. So a shortened version of your test data would look like this:
$myDataStore = array(
"2" => "19",
"4" => "19",
"6" => "19",
"8" => "21",
"10" => "21",
"12" => "22",
"14" => "23",
"16" => "23",
"18" => "23",
"20" => "23"
);
Now you need a function to calculate your sum given a range as defined by starting and ending numbers.
function getRangeTotal($array, $startNumber, $endNumber){
$total = 0;
foreach($array as $key => $value){
if($key >= $startNumber && $key <= $endNumber){
$total = $total + $value;
}
}
return $total;
}
If you run the above function
getRangeTotal($myDataStore, 6, 12);
You'll get 83
Here is how you can do this using foreach
// first store you data to an array.
$kv = array(
2 => 19, 4 => 19, 6 => 19, 8 => 21,
10 => 21, 12 => 22, 14 => 23, 16 => 23,
18 => 23, 20 => 33, 22 => 37, 24 => 42,
26 => 45, 28 => 48, 30 => 50, 32 => 55,
34 => 61, 36 => 66, 38 => 72, 40 => 78,
42 => 155, 44 => 179, 46 => 202,48 => 233,
50 => 360
);
$start = 16;
$end = 32;
$sum = 0; //variable to keep sum
$n = 0; //variable to keep count
//loop through the array
foreach ($kv as $k => $v){
if ($k >=$start && $k <= $end){ //if key is in your range
$sum += $v; //add value to sum
$n ++; // increment count
}
}
$v = 11.218*pow(M_E,0.057*$n); //calculate the approximate values
echo "$sum\n$v\n";
Also refer to : pow and Predefined Constants

PHP Array Sorting And Getting Max Values from a Matrix

I'm hoping there's an easy way to do this without tons and tons of loops.
I have a matrix in the following manner:
Weight1 Weight2 Weight3 .... WeightN
Jan 1 3 5 4
Feb 10 12 15 11
Mar 5 7 4 3
Apr 10 15 7 3
Assuming the following array:
$arrayMonths = array(
'jan' => array(1, 3, 5,4)
'feb' => array(10,12,15,11)
'mar' => array(5, 7, 4, 3)
'apr' => array(10,15,7,3)
);
What is the best way for me to get the the max for each row:
jan = 5
feb = 15
mar = 7
apr = 15
Thanks
Use the max() function:
foreach($arrayMonths as $month => $row)
{
echo $month . ": " . max($row) . "<br />";
}
foreach ($arrayMonths as $month => $array) {
// store the max of each month for later
// $max['jan'] = 5
$max[$month] = max($array);
// or print it out
echo $month. ' => '. max($array);
}
From the max() PHP Docs
If you need to go further with sorting you can check out here for more information
PHP Array Sorting
Try this:
<?php
function getMax( $v, $k ) {
global $arrayMonths;
$arrayMonths[ 'max' ][$k] = max($v);
}
$arrayMonths = array(
'jan' => array(1, 3, 5,4),
'feb' => array(10,12,15,11),
'mar' => array(5, 7, 4, 3),
'apr' => array(10,15,7,3)
);
array_walk( $arrayMonths, 'getMax' );
// Now $arrayMonths['max'] will contain all max values for each key
?>
Hope this helps.
I found the perfect algorithm: Basically use a recursive function which goes through all permutations.

Categories