Newbie here in PHP programming. I have some question regarding arrays. How can I get the difference between adjacent values in an array?
$value = array(2, 5, 9, 10, 19);
How do I get the difference between the 2 and 5? Then 9 and 5? Then 10 and 9, then 19 and 10?
The reason I want to get the difference is to draw a stack graph. And each stack will depend on each difference.
Update:
Hi, sorry guys, totally lost on this one. It seems that I can't do it the way I want. Please click here to see the similar stacked graph I want. How do I create a scale for my arrays so that when I plot the stacked graph it will automatically adjust to its value?
Here is the code that I am trying to use.
<?php
$img_width=200;
$img_height=425;
$img=imagecreatetruecolor($img_width,$img_height);
$bar_color=imagecolorallocate($img,0,64,128);
$line_color=imagecolorallocate($img,220,220,220);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
$value = array(116,160,210,269,325,425);
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
imageline($img,0,$value[$i]-$value[$i-1],$img_width,$value[$i]-$value[$i-1],$line_color);
}
header("Content-type:image/png");
imagepng($img);
?>
I appreciate all your help.
Just use a simple for loop:
$diffs = array();
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
}
#Gumbo you added $diffs = array(); it has to be $value
<?php
$value = array(2,5,9,10,19);
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
}
echo "<pre>";
print_r($diffs);
echo "</pre>";
?>
Related
I have an array like this:
$datas = array(54,12,61,98,88,
92,45,22,13,36);
I want to write a loop which can deduct values of an array like below and show it with echo:
$datas[5]-$datas[0] for this line the result will be 92-54 "38"
$datas[6]-$datas[1] for this line the result will be 45-12 "33"
$datas[7]-$datas[2] ... "-39"
my codes are:
<?php
$smonth1= 0;
$emonth1=5;
for ($i = 5; $i > 0; $i-- ) {
$result = array_diff($datas[$emonth1], $datas[$smonth1]);
echo (implode ($result))."<br/>" ;
$smonth1++ ;
$emonth1++;
}
?>
but I couldn't get the result I don't know why. I am fresh in php. Can you help me??
Assuming the input array always has an even number of values in it (which I think is the only way this scenario could logically work), then you can simply count how many items are in the array, and then loop through it, taking the nth item and subtracting it from the n+(total / 2)th item.
$data = array(54,12,61,98,88,
92,45,22,13,36);
$halfway = count($data)/ 2;
for ($i = 0; $i < $halfway; $i++)
{
$j = $i + $halfway;
echo $data[$j] - $data[$i].PHP_EOL;
}
Demo: https://3v4l.org/ictDT
Basically, you want something like this
<?php
$data = [
54, 12, 61, 98, 88,
92, 45, 22, 13, 36
];
$offset = 5;
for ($i = 0; $i + $offset < count($data); $i++) {
echo $data[$i + $offset] - $data[$i];
echo "\n"; // or <br/> if you run it in browser
}
My goal is to get the count of the max amount of numbers, which are increasing in a row in a certain date range. Below is an array. The correct answer of the max amount increasing numbers between 01/13/2021 and 02/17/2021 would be 5 as the numbers 3,8,10,13,15 are growing in that date range.
I managed to search in a certain period of time...
$row = array(
array('01/02/2021', 1),
array('01/13/2021', 4),
array('01/15/2021', 6),
array('01/19/2021', 9),
array('01/30/2021', 5),
array('02/03/2021', 4),
array('02/11/2021', 3),
array('02/12/2021', 8),
array('02/15/2021', 10),
array('02/16/2021', 13),
array('02/17/2021', 15),
array('02/18/2021', 16)
);
$startDateNew = date('m/d/Y', strtotime("2021-01-13"));
$endDateNew = date('m/d/Y', strtotime("2021-02-17"));
foreach($row as $x) {
if(($x[0]>=$startDateNew)&&($x[0]<=$endDateNew) ){
echo 'Found ';
print_r($x);
}
}
I managed also to search the first increasing number sequence in a one-dimensional array as the answer of the code below is 4. But I didn't succeed to continue this search after if-sentence to the next increasing sequence(and the next and the next), so that the correct answer would be 5 in this case.
Including to that, these two searches should combine somehow together.
$numbers = array(1,4,6,9,5,4,3,8,10,13,15);
function LCIS($numbers) {
$counter = 1;
$answer = 1;
for($i = 0; $i < count($numbers) -1; $i++) { //
if ($numbers[$i] < $numbers[$i+1]) { //comparing array indexes together
$counter++; //
$answer = max($answer, $counter); //this doesn't do anything spectacular right now...
continue;
}else {
$counter == 1;
}
return $answer;
}
}
echo LCIS($numbers);
I'm very beginner at php coding. Could you help me, please?
Well if you have already 2 parts of the code you just need to combine them together. What I suggest is to feed the $numbers array during checking if the $row array element is in certain date range.
function LCIS($numbers)
{
$counter = 1;
$answer = 1;
for ($i = 0; $i < count($numbers) - 1; $i++) { //
if ($numbers[$i] < $numbers[$i + 1]) { //comparing array indexes together
$counter++; //
$answer = max($answer, $counter); //this doesn't do anything spectacular right now...
} else {
$counter = 1;
}
}
return $answer;
}
$row = array(
array('01/02/2021', 1),
array('01/13/2021', 4),
array('01/15/2021', 6),
array('01/19/2021', 9),
array('01/30/2021', 5),
array('02/03/2021', 4),
array('02/11/2021', 3),
array('02/12/2021', 8),
array('02/15/2021', 10),
array('02/16/2021', 13),
array('02/17/2021', 15),
array('02/18/2021', 16)
);
$numbers = array();
$startDateNew = date('m/d/Y', strtotime("2021-01-13"));
$endDateNew = date('m/d/Y', strtotime("2021-02-17"));
foreach($row as $x) {
if(($x[0]>=$startDateNew)&&($x[0]<=$endDateNew) ){
$numbers[] = $x[1];
}
}
echo LCIS($numbers);
BTW: I have modified a little your LCIS function and it will run faster and more correct.
Recently, my exams got over. My last exam was based on PHP. I got the following question for my exam:
"Convert the following script using for loop without affecting the output:-"
<?php
//Convert into for loop
$x = 0;
$count = 10;
do
{
echo ($count. "<BR>");
$count = $count - 2;
$x = $x + $count;
}
while($count < 1)
echo ($x);
?>
Please help me as my computer sir is out of station and I am really puzzled by it.
Well, If I understand well, You have to use "for" loop, instead of "do...while", but the printed text must not change.
Try:
$count = 10;
$x = 0;
$firstRun = true;
for(; $count > 1 || $firstRun;) {
$firstRun = false;
echo ($count . "<BR>");
$count -= 2;
$x = $x + $count;
}
echo ($x);
By the way loop is unnecessary, because $count will be greater than 1 after the first loop, so the while will get false.
EDIT
$firstRun to avoid infiniteLoop
$count in loop
EDIT
Fixed code for new requirement
Removed unnecessary code
Hmmm I don't know if your teacher wanted to own you... but the do{} will execute only once since $count is never < 1.
The output of your teacher's code is:
10
8
I presume there was a mistake in the code and the while would be while($count > 1) which would make more sense (since it's weird to ask for a loop to output only 10 8) and would result in this output:
10
8
6
4
2
20
Then a good for() loop would have been :
$x = 0;
$count = 10;
for($i = $count; $i > 1; $i -= 2)
{
$count -= 2;
echo $i . "<br>";
$x += $count;
}
echo $x;
Which will output the same values.
If you can, ask your teacher for this, and comment the answer ^^ ahahah
PHP: Here is what I am supposed to accomplish. And below is my code. I am definitely missing something..
Write a FOR loop using an array that prints “The product of first 10 numbers is ” followed by the product of numbers 1 through 10. Here’s a hint: do NOT begin counter at 0 or else the product will be a zero!
<?php
$numbers = 0;
$numbers = range(1 , 10);
$arrlength = count($numbers);
for ($x = 1; $x <= $arrlength; $x++) {
$numbers[$x] = $numbers + $x;
}
echo "The product of first 10 numbers is $numbers.<br>";
?>
Essentially what you're doing is creating a factorial function for a limited case. The problems with your implementation are that you seemed to re-use the $numbers variable when it should be one variable to hold the product and one for the array 1-10. The second issue is in your loop where you loop from 1-10 but what you probably really want to do is loop from 0-9 which are the array indices so that you can get the values from the array like this:
<?php
$prod = 1;
$numbers = range(1 , 10);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
$prod *= $numbers[$x];
}
echo "The product of first 10 numbers is $prod.<br>";
?>
The output of which will be:
The product of first 10 numbers is 3628800.
Please note I have not tested this code, but I'm sure it should work.
<?php
$numbers = range(1, 10);
$product = 1;
for ($i = 0; $i < count($numbers); $i++) {
$product *= $numbers[$i];
}
echo 'The product of the first 10 numbers is ' . $product . '<br />';
?>
First, I have been programming in PHP for all of two weeks. So, please excuse my ignorance.
Here is my problem. I am trying to find essentially the running average of multiple difference values within an array. I think I have figured out how to get what I want, but the code is really inefficient and takes several seconds in a small test array (n = 20). My production array will be around n = 1000. I suspect that there is a much more efficient way to get what I want.
Here is what I am conceptually trying to do:
array = (20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
newarray = (20-19, 19-18, 18-17, 17-16, 16-15, ... 2-1);
newarray2 = (20-18, 19-17, 18-16, 17-15, 16-14 ... 3-1);
newarray3 = (20-17, 19-16, 18-15, 17-14, ... 4-1);
differenceaverage = (sum of (20-19, 20-18, 20-17)/3, sum of (19-18, 19-17, 19-16)/3, etc.)
Here is the code that I used to generate the average:
$n=count($appArrayqb);
for($i = 1; $i<=($n-1); $i++){
$diff[]= $appArrayqb[$i-1]-$appArrayqb[$i];
}
for($i = 1; $i<=($n-2); $i++){
$diff2[]= $appArrayqb[$i-1]-$appArrayqb[$i+1];
}
for($i = 1; $i<=($n-3); $i++){
$diff3[]= $appArrayqb[$i-1]-$appArrayqb[$i+2];
}
$VOcomb = array($diff,$diff2,$diff3)
$sumVO = array_map ("sum", $VOcomb[0], $VOcomb[1], $VOcomb[2])
function sum ($arr1, $arr2, $arr3){
return($arr1+$arr2+$arr3);
}
$counts = array();
foreach($VOcomb as $item){
foreach($item as $k=>$v){
if(isset($item[$k])){
$counts[$k]++;
}
}
}
$VOarray=array($sumVO,$counts);
$VO = array_map("VO", $VOarray[0],$VOarray[1]);
function VO($arr1, $arr2) {
return($arr1/$arr2);
}
Incidentally, while the code works, I get an undefined offset notice when I run the count loop because it is trying to count values that don't exist in $item array. So I have three related questions:
1.) Is there a better way to calculate the differences?
2.) Is there a better way to get the count of the differences so I don't wind up with an undefined offset?
3.) Is the array_map function the best function to use to get the sums and averages?
Thanks!
Try this simplified code that seems to give the same result as yours:
It works by building the result array element by element, calculating the differences and averages as needed instead of creating arrays of differences and mapping over them.
// Calculate average differences between E(n) and E(n+1), E(n+2), and E(n+3)
// in an array
function diffaverages($appArrayqb) {
$n = count($appArrayqb);
$diffAvg = array();
for($i = 0; $i+1 < $n; $i++){
$diffsum = 0;
$count = 0;
for($j = 1; $j <= 3 && $i+$j < $n; $j++){
$diffsum += $appArrayqb[$i] - $appArrayqb[$i+$j];
$count++;
}
$diffAvg[] = $diffsum / $count;
}
return $diffAvg;
}