How to combine all the arrays into one total result?
now the results with echo like 252 299 i need get total result 252+299 and get from echo 551
<?php
$videoListt = array_merge($topchan1["items"],$topchan2["items"]);
for( $i= 0 ; $i <= count($videoListt)-1 ; $i++ )
{
echo $videoListt[$i]["statistics"]["subscriberCount"];
}
?>
You can increment a variable from within each iteration of your loop, then output it at the end:
$subscriberCount = 0;
for ($i = 0; $i <= count($videoListt) - 1; $i++) {
$subscriberCount += $videoListt[$i]['statistics']['subscriberCount'];
}
echo $subscriberCount;
Related
I need to print a number pattern like this:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123
My code:
for($i=1; $i<=4; ++$i) {
for($j=1; $j<=$i; ++$j) {
echo $j;
}
echo ("<br/>");
}
for($i=2; $i<=4; ++$i) {
for($j=2; $j<=$i; ++$j) {
echo $j;
}
echo ("<br/>");
}
I don't know how to recycle to the first number after the max number is reached. Since my max number is 4, 1 should used instead of 5 (and 2 for 6 and 3 for 7).
You loop from 1 to 4, and subtract 4 if the value is bigger than 4.
This is for 2, 23, 234, 2341:
for ($i = 1; $i <= 4; $i++) {
for ($j = 1; $j <= $i; $j++) {
$value = $j + 1; // or +2, or +3
echo $value > 4 ? $value - 4 : $value;
}
echo "\n";
}
And this would generate all output within one big loop:
$max = 4;
for ($start = 0; $start < $max; $start++) {
for ($i = 1; $i <= $max; $i++) {
for ($j = 1; $j <= $i; $j++) {
$value = $j + $start;
echo $value > $max ? $value - $max : $value;
}
echo "\n";
}
}
Whenever you need circular array access, it is a good idea to implement a modulus calculation. To set this up, use a range between 0 and 3 instead of using 1 and 4. A modulus calculation can return a 0 result, so the formula must be prepared to handle this value. To adjust the value, just add 1 after the modulus calculation to generate numbers between 1 and 4.
Accumulate strings of numbers as desired and reset the string upon each start of the outer loop.
Below proves that you do not need three nested loops for this task.
Code: (Demo)
for ($i = 0; $i < 4; ++$i) {
for ($s = '', $j = 0; $j < 4; ++$j) {
$s .= ($i + $j) % 4 + 1;
echo $s . "\n";
}
}
Output:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123
I am working with some legacy PHP code so re-writing this isn't an option at this point but I have a dropdown for number of years and months of employment and currently they go from 0 - 11, 0 - 65. Can a PHP loop array numbers starting at NULL, which adds -Select- as the default forcing user to make a selection, but also have 0 as the starting number?
I've tried:
for ($i = NULL; $i <= 11; $i++) {
echo $i;
}
But 0 is no loner an option
This is what I have currently:
for ($i = 0; $i <= 11; $i++) {
echo $i;
}
I need it to display as:
-Select-
0
1
2
3
4
etc
Echo the text before echoing the numbers using a loop.
<?php
echo '-Select-';
for($i = 0; $i <= 11; ++$i) {
echo $i;
}
NULL++ would not increase the value of NULL, which is essentially nothing. Why not start at -1, and if ($i == -1) then echo select?
It would look like this:
for ($i = -1; $i <= 11; $i++) {
if ($i == -1) {
echo '-Select-';
} else {
echo $i;
}
}
What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102
I want to turn the $i variable value to start counting from 1 if the given value is greater than 10:
here is what i am trying to achieve
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i < $givenValue; $i++) {
if ($givenValue > 10){
$i = 1;
}
echo $i."<br>";
}
?>
This is how i want my result to look like
output: 1
output: 2
output: 3
output: 4
output: 5
output: 6
output: 7
output: 8
output: 9
output: 10
output: 1
output: 2
output: 3
output: 4
output: 5
in for loop body
Any help is welcome
You can use modulo calculation to get the result you want.
I also changed your if from $givenvalue to $i as $givenvalue will "always" be 10+.
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($i > 10){
Echo $i%10 . "\n";
}else{
echo $i . "\n";
}
}
https://3v4l.org/5afc5
Another option, if that is possible for you, is to start at zero and only use modulo calculation and add one to it to get the same result.
This also means I need to stop the loop at <$givenvalue as your original code shows.
$givenValue = 15; //number of x value
for ($i = 0; $i < $givenValue; $i++) {
Echo $i%10+1 . "\n";
}
https://3v4l.org/r0sgA
A method that uses less looping is to add 10 to the loop on each iteration and create the values using range().
Then add them to the array with array_merge, and output with implode.
$givenValue = 47; //number of x value
$breakpoint = 10;
$arr=[];
For($i = $breakpoint; $i< $givenValue;){
// Add new values from 1-$breakpoint in array
$arr = array_merge($arr, range(1,$breakpoint));
$i +=$breakpoint;
}
// Loop will exit before all values been collected
// Add the rest of the values
$arr = array_merge($arr, range(1,$givenValue-($i-10)));
// Echo the values in array
Echo implode("\n", $arr);
https://3v4l.org/jGsO4
Your code can be written like this:
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++)
{
if ($i > 10)
{
$i = 1;
$givenValue-=10;
}
echo "output: $i\n";
}
?>
http://sandbox.onlinephpfunctions.com/code/ed34d8dcd12a9a5a866b73338ad1209f55298519
You are resenting the counter, I would expect the behaviour you have. To do what you want add another counter to the mix
$j=1;
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($j > 10){
$j = 1;
}
echo $j."\n";
++$j;
}
You also had several missing ;
Output:
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
If you want to end on 5 you have to do 16 as the $givenValue or change it to <= less than or equal
See it here live
See what I have now, the $i variable counts to the $givenValue then the $j variable counts along side it, but with a range of 1-10 ( resets to 1 after 10 )
I am trying to make a triangular-shaped set of lines of decreasing numbers like this :
5
45
345
2345
12345
I tried this :
for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br>";
}
But it is printing the low number first and appending increasing numbers like this :
1
12
123
1234
12345
The inner loop needs to count down instead of up.
You can either subtract the outer loop's variable from the limit to get the starting point and count down:
for ($i = 0; $i < 5; $i++)
{
for ($j = 5 - $i; $j > 0; $j--)
{
echo $j;
}
echo "<br>";
}
or change the outer loop to count down from the limit as well.
for ($i = 5; $i >= 1; $i--)
{
for ($j = $i; $j >= 1; $j--)
{
echo $j;
}
echo "<br>";
}
This is pretty straightforward:
$max = 5;
echo "<pre>";
for($line=0; $line<$max; $line++) {
$min_this_line = $max-$line;
for($num = $min_this_line; $num <= $max; $num++) {
echo $num;
}
echo "\n";
}
echo "</pre>";
Output:
5
45
345
2345
12345
I think I would declare the $peak value, then use a for() loop to decrement the counter down to 1, and use implode() and range() to build the respective strings inside the loop.
This isn't going to outperform two for() loops, but for relatively small $peak values, no one is going to notice any performance hit.
Code: (Demo)
$peak = 5;
for ($i = $peak; $i; --$i) {
echo implode(range($i, $peak)) , "\n";
}
or with two loops: (Demo)
Decrement the outer loop and increment the inner loop.
$peak = 5;
for ($i = $peak; $i; --$i) {
for ($n = $i; $n <= $peak; ++$n) {
echo $n;
}
echo "\n";
}
Both output:
5
45
345
2345
12345