array within indexed numbers - php

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.

Related

How to deduct specific array "values from another values" in PHP?

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 in php

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

Decoding function to better understand

I am revisiting PHP and want to relearn where i lack and i found one problem, i am unable to understand the following code, where as it should output 6 according to the quiz, i got it from but i broke it down to simple pieces and commented out to better understand, according to me the value of $sum should be 4, but what i am doing wrong, maybe my breakdown is wrong?
$numbers = array(1,2,3,4);
$total = count($numbers);
//$total = 4
$sum = 0;
$output = "";
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
//0+1 = 1
//0+2 = 2
//0+3 = 3
//0+4 = 4
if ($i < $total) {
$sum = $sum + $number;
//1st time loop = 0 < 4 false
//2nd time loop = 0 < 1 false
//3rd time loop = 0 < 2 false
//5th time loop = 0 < 3 false
//6th time loop = 4 = 4 true
//$sum + $number
//0 + 4
//4
}
}
echo $sum;
This is very basic question and might get down vote but it is also a strong backbone for people who want to become PHP developer.
You don't understand the last part in the loop. It actually goes like this now:
if($i < $total) {
$sum = $sum + $number;
//1st time loop: $sum is 0. $sum + 1 = 1. $sum is now 1.
//2nd time loop: $sum is 1. $sum + 2 = 3. $sum is now 3.
//3rd time loop: $sum is 3. $sum + 3 = 6. $sum is now 6.
//4th time loop: it doesn't get here. $i (4) < $total (4)
//This is false, so it doesn't execute this block.
}
echo $sum; // Output: 6
I altered your script a little so that it will print out what it's doing as it goes. I find it useful to do this kind of thing if I'm having a hard time thinking through a problem.
$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$i = 0;
$j = 0;
foreach($numbers as $number) {
$i = $i + 1;
echo "Iteration $j: \$i +1 is $i, \$sum is $sum, \$number is $number";
if ($i < $total) {
$sum = $sum + $number;
echo ", \$i is less than \$total ($total), so \$sum + \$number is: $sum";
} else {
echo ", \$i is not less than \$total ($total), so \$sum will not be increased.";
}
echo '<br>'; // or a new line if it's CLI
$j++;
}
echo $sum;
Lets Explain
Your initial value of $i is 0 but when you start looping you increment it by 1, so the start value of $i is 1.
When checking the condition you did't use equal sign to check for the last value whether you start value is 1. So its clear that your loop must be run for 1 less of total.
$i = 0;
foreach($numbers as $number) {
$i += 1;
if ($i < $total)
$sum += $number;
}
echo $sum;
Analysis
Step: 1 / 4
The value of $number is: 1 And The value of $i is: 1
Step: 2 / 4
The value of $number is: 2 And The value of $i is: 2
Step: 3 / 4
The value of $number is: 3 And The value of $i is: 3
When the loop again go for a check the value of $i increased by 1 and at 4. So trying to match the condition if ($i < $total), where the value of $i and $total is equal, so it will return false. So the loop only run for 3 time.
Result
6

loop through multi-dimensional array while restarting interation from zero

for example i have array like this
$x = array(
array(
array(1, 2, 3, 4),
array(5, 6, 7, 8)
)
)
and then i loop the array like this
$j = 0;
for($i = 0; $i < count($x[0][$j]); $i++){
}
if some condition occured
can i increment $j and continue/reset loop $i from 0? so $x[0][$j][0] = 5. how to do that?
currently, i tried this
$j = 0;
for($i = 0; $i < count($x[0][$j]); $i++){
if(/*somecondition*/){
$j++;
continue;
}
}
If you just want continue with $i = 0 when a condition is true you can set it to -1 when the condition is true so at the next iteration it's 0!
Like this:
if(/*somecondition*/){
$j++;
$i = -1; //So in the next iteration when it gets incrementet by 1 it's 0
}

Creating an N x N array of integers that "staircase" and "wrap around"

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);

Categories