PHP: loop through an array give wrong result [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have following code:
$source_stat_array = array(0 => 'clicks', 1 => 'impr', 2 => 'spend');
$count = count($source_stat_array);
for ($i = 0; $i < count; $i++) {
echo $source_stat_array[$i++];
}
the result i get is following:
clicks spend
instead of i need
clicks impr spend
can you answer me what is wrong with code?

You are incrementing the value of $i twice.
Try the below code:
for ($i = 0; $i < $count; $i++) {
echo $source_stat_array[$i];
}

You can use foreach it will iterate through each key
foreach($source_stat_array as $key => $value){
echo $value;
}
Read more about foreach , manual

Related

PHP for loop increment by 100000 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to loop through $i which starts from 790000000 and echo every 100000 up to 799999999:
790000000
790100000
790200000
790300000
790400000
...
I tried this code but it didn't work:
for ($i=790000000; $i<=800000000; $i+100000) {
echo $i . '<br>';
}
For loop wrong code, you are missing actual code update as $i+100000 does not update variable. Use $i += 100000 instead.
// Here is problem in your code
for ($i=790000000; $i<=800000000; $i += 100000) {
echo $i . '<br>';
}
Update your loop using += instead of i.
Only using + will not increment your $i variable value.
for ($i=790000000; $i<=800000000; $i+=100000) {
echo $i . '<br>';
}

Having Issue On PHP Increment [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Can you please take a look at this demo and let me know why I am not able to increment the $start as the output is looks like 88888 only
<?php
for($i = 1; $i<= 5; $i++){
$start = 8;
echo $start;
$start++;
}
?>
That is because you always assign your value inside loop , take it out
<?php
$start = 8;
for($i = 1; $i<= 5; $i++){
echo $start;
$start++;
}
?>
If you want to increment the value of start, you must declare it outside of the for loop.
<?php
$start = 8;
for($i = 1; $i<= 5; $i++){
echo $start;
$start++;
}
?>

How to sort an empty array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
How do you sort this empty array using foreach loop to output these 25 numbers in order?
Example outcome would be: 1 1 1 1 1 2 2 2 2 3 3 3 3 3 4 4 4 4
<?php
$array = array();
for($counter = 0; $counter <=25; $counter++){
$die = rand(1, 10);
$int[$counter] = $die;
echo " $die ";
}
sort($die);
foreach ($die as $value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}
?>
<?php
$array = array();
for ($i = 0; $i <=25; $i++) {
echo $array[] = rand(1, 10);
}
sort($array);
foreach ($array as $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
}

for loop doesn't after looping throw the script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have php script to do for loop, it supposed to loop over the code once and then exit,but theses not happening, it keep looping till i exited manually.
my code
<?php
session_start();
?>
<?php
ignore_user_abort(true);
for( $x=0; $x< 50; )
{
$_SESSION['timeout'] = time();
$st = $_SESSION['timeout'] + 1;
session_unset();
$_SESSION['timeout'] = time();
$st = $_SESSION['timeout'] + 1;
print_r("$st\n");
}
if(time() < $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test');
}
?>
Below is an infite loop. $x always stays at 0.
for( $x=0; $x< 50; )
You need to do:
for( $x=0; $x< 50; $x++)
php man "for"

undefined offet error in arrays in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
I am getting a problem please help me.
this is my array
Array
(
[0] => subject7
[1] => subject6
[2] => subject5
[3] => subject3
[4] => subject2
)
and my array size is 5
$sub= array();
for($j=0; $j<=$size; $j++)
{
$sub[] = $subject_value[$subject[$j]];
}
but it is giving me a error message Undefined offset: 5
The error occured because you dont have a 5th element in the array.So change the code like the following
$sub= array();
for($j=0; $j<$size; $j++)
{
$sub[] = $subject_value[$subject[$j]];
}
In this case
$j <= $size
will iterate the loop 6 times and so it looks for the 6th element which is $size[5] since the key starts from 0 and you dont have a 6th or $size[5] element in the array.So you will get the error.So you need to iterate the loop 5 times as your array size is 5.So you have to change the condition as $j < $size in the for loop.
change
$j<=$size
to
$j<$size
do
for($j=0; $j<$size; $j++)
$sub= array();
for($j=0; $j<=$size; $j++)
{
$sub[] = $subject_value[$subject[$j]];
}
should be
$sub= array();
for($j=0; $j<$size; $j++)
{
$sub[] = $subject_value[$subject[$j]];
}
You are using the <= operator and it also check the index number 5 and make the condition true for this index. Even though there is only indexs till 4.
for($j=0; $j<$size; $j++)
change it to < operator according to above code as there is only index 4 from 0 and then it make 5 array element.
To avoid such common mistakes, you could use a foreach loop:
foreach($subject as $item){
//work here
}
This way you will never try to access a non-existing element (in your case $subject[5]).

Categories