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
}
I want to count number of even,number of odd.If there 2 even ,i count only one even,If there 2 odd i count
only one number of odd
<?php
$myarray = array(5,5,0,1,2,1,1,6,1);
for ($i = 0; $i < count($myarray); $i++) {
echo "Index ", $i, ", value ", $myarray[$i], ": ";
if ($myarray[$i] % 2 == 0) {
echo "even\n";
}
else {
echo "odd\n";
}
}
?>
input =[5,5,0,1,2,1,1,6,1]
output = 5 :1(total(5) odd one),
1:2 (total(1) odd two)
Use two variables that keep track of your odds and evens when you loop through your array.
$my_array = array( 5, 5, 0, 1, 2, 1, 1, 6, 1 );
$odd = $even = 0;
foreach ( $my_array as $number ) {
$number % 2 == 0 ? $even++ : $odd++;
}
printf( 'Odd: %s | Even: %s', $odd, $even );
<?php
$num = array(23,12,11,9,6,7,4,5,3);
$n=count($num);
$even = 0;
$odd = 0;
for( $i = 0 ; $i < $n; $i++)
{
if ($num[$i] & 1 == 1)
$odd ++ ;
else
$even ++ ;
}
echo "Number of even elements = $even
and Number of odd elements = $odd" ;
?>
Create counters $evens and odds. Then Loop through elements in array check if number is odd
using Ternary Operator (condition) ? true : false;, and $num & 1 return true if number is odd.
$nums = array(5,5,0,1,2,1,1,6,1);
$odds = $evens = 0;
foreach ($nums as $num) {
$num & 1 ? ++$odds : ++$evens;
}
echo "Odds: $odds, Evens: $evens"; //Odds: 6, Evens: 3
Just count only number of odds, Because If we count number of Odds in array, It would be easy to know number of Evens by subtract number of elements in array with number of Odds
$nums = array(5,5,0,1,2,1,1,6,1);
$odds = 0;
foreach ($nums as $num) {
$odds += $num % 2;
}
$evens = count($nums)-$odds;
echo "Odds: $odds, Evens: $evens"; //Odds: 6, Evens: 3
I tried to test a new thing and just saw an unusual output I am unable to get what exactly is happening within the loop can anyone explain me what happens if we take an array and assign that array to a variable we run a for loop and we provide $array and index of $array with the inititlizer for forloop something like this $array[$array[$i]] so I mean I am confused to totally explain but can you review the code and let me know what exactly is happening
$array = array(1, 2, 3, 5, 8, 13, 21, 34, 55);
$sum = 0;
for($i = 0; $i < 5; $i++) {
$sum += $array[$array[$i]];
}
echo $sum ;
The output 78 as it added the value but if i remove $sum+= and write down like this
echo $array[$array[$i]] . "<br />";
so now what here is I will be getting output like this
2
3
5
13
55
I am unable to get what exactly hapend within this loop
You are using the value of the array to access the key.
$array = array(1, 2, 3, 5, 8, 13, 21, 34, 55);
$sum = 0;
for($i = 0; $i < 5; $i++) {
$sum += $array[$array[$i]] . "<br />";
}
echo $sum ;
Loop 1:
$i = 0;
$sum += $array[$array[0]];
$sum += $array[1];
$sum += 2;
Loop 2:
$i = 1;
$sum += $array[$array[1]];
$sum += $array[2];
$sum += 3;
Loop 3:
$i = 2;
$sum += $array[$array[2]];
$sum += $array[3];
$sum += 5;
Loop 4:
$i = 3;
$sum += $array[$array[3]];
$sum += $array[5];
$sum += 13;
Loop 5:
$i = 4;
$sum += $array[$array[4]];
$sum += $array[8];
$sum += 55;
So...
$sum = 2 + 3 + 5 + 13 + 55 = 80
For $i = 0
what is $array[$i] -> $array[0] -> 1.
what is $array[$array[$i]] -> $array[1] -> 2.
And so on.
First iteration ($i = 0)
$array[$i] = $array[0] = 1
and
$array[1] = 2
Second iteration ($i = 1)
$array[$i] = $array[1] = 2
and
$array[2] = 3
Third iteration ($i = 2)
$array[$i] = $array[2] = 3
and
$array[3] = 5
Fourth iteration ($i = 3)
$array[$i] = $array[3] = 5
and
$array[5] = 13
Fifth iteration ($i = 4)
$array[$i] = $array[4] = 8
and
$array[8] = 55
When $i = 0 $array[$array[$i]] becomes $array[$array[0]] which is $array[1] since $array[0] = 1. So $array[1] evaluates to 2.
Keep thinking the same way.
For example, for $i = 4 $array[$array[$i]] becomes $array[$array[4]] which is $array[8] since $array[4] = 8. So $array[8] evaluates to 55.
Think step by step.
I have an array that I am looping through and breaking up into chunks of 50. However occasionally the number of items inside that array are more than what fits inside that chunk of 50 ex.:
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
print_r($j); // do something here with the 50 rows
$j = null;
}
}
The problem here is that this will not print anything after 201. I know there is some algebraic math involved in solving this but I am drawing a blank. Its times like these where I really wish I had paid attention in math class back in high school.
I think array_chunk fits up your requirement and no maths required.
$result_array = array_chunk($array, 50, true);
Add additional condition
if ($i % 50 == 1 || count($array)-1 == $i)
You just have to redeclare the array is my guess:
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
print_r($j); // do something here with the 50 rows
$j = array() ;
}
}
Once you perform $j = null there is no way you can do $j[] = $i
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
doSomething($j); // do something here with the 50 rows
$j = array(); // reset the array
}
}
doSomething($j); // with the last 20 entries
After your loop is finished, you will have the remaining 201 through 220 entries in $j, so just do your stuff again.
array_chunk
might be useful. Basically splits the array into chunks returning a multi dimensional array
I am having trouble trying to figure out how to get data ordered like below. The total numbers don't matter; it would follow the same pattern from any number in the logical order of 0, 1, 2, 3, 4, 5, 6, etc. So essentially, starting at 0, 2, 3, 4, etc. where 1 would be placed after the maximum number, and where 0 can be a variable I set statically. I am having issues with progressing all the way to max number and then continuing, e.g.
..., 97, 98, 99, 100, 1, 2, ...
and then progressing with the order,
..., 98, 99, 100, 1, 2, 3, ...
and so on until 1, 2, 3, 4, 5, 6, ...
and store this all into the multidimensional array below.
$set = array(
array('0','0','0','0','0','0','0','0','0','0','0'),
array('0','2','3','4','5','6','7','8','9','10','1'),
array('0','3','4','5','6','7','8','9','10','1','2'),
array('0','4','5','6','7','8','9','10','1','2','3'),
array('0','5','6','7','8','9','10','1','2','3','4'),
array('0','6','7','8','9','10','1','2','3','4','5'),
array('0','7','8','9','10','1','2','3','4','5','6'),
array('0','8','9','10','1','2','3','4','5','6','7'),
array('0','9','10','1','2','3','4','5','6','7','8'),
array('0','10','1','2','3','4','5','6','7','8','9'),
array('0','1','2','3','4','5','6','7','8','9','10'),
);
I did the above because I couldn't figure out a looping pattern; if I could figure that out I wouldn't need to enter in the data manually and could create a form by which any number could be chosen, following this pattern.
Notice that other than the first row and column, each row is just the previous shifted left, with the next value added on:
$max = 10;
// First row (full of 0)
$set = array(array_fill(0, $max + 1, 0));
$row = array();
for($i = 1; $i <= $max; $i++)
$row[] = $i;
$row[] = 1; // $row = [2,3,4,...,$max,1]
for($i = 0; $i < $max; $i++){
$set[] = array_merge(array(0), $row);
$row = array_map(function($x) use ($max){ // Requires PHP 5.3
$result = ($x + 1) % $max;
return 0 === $result ? $max : $result;
}, $row);
}
Codepad
It's of course fairly trivial to make this store strings instead of integers if you require that.
$array = array();
$max = 10;
for ($i = 0; $i < $max; $i++)
{
$num = $i + 2;
$array[$i][] = 0;
for ($j = 0; $j < $max; $j++)
{
if ($num == $max + 1)
$num = 1;
$array[$i][] = $num;
$num++;
}
}
var_dump($array);