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]).
Related
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
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>';
}
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>';
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What's the easiest way to find the first zero bit with PHP?
For example, say I have an integer 47 which is 111101, how can I find out that the 5th bit is the first unset bit? This needs to work to cater for different integers.
$value = 47;
$i = $j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
$i++;
}
echo "bit $i is 0";
If you want to eliminate the use of $i as a counter, you can do a little bit of extra math:
$value = 47;
$j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
}
echo "bit ", (log($j) / log(2) + 1), " is 0", PHP_EOL;
The +1 is necessary because you're starting your binary as bit 1 rather than as bit 0
Use decbin to return a string of 0 and 1.
Then, use strpos to find the first 0 caracters.
$str = decbin(47);
$result = strpos($str, '0');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a scenario: The $numbers array in the code below contains one thousand numbers(0 - 1000). I need to count how many of these numbers fit into each of the following categories:
how many of them are between 1 and 1000 inclusive,
how many of them are less than 1, and
how many of them are greater than 1000.
I have created a foreach loop to look at each number one after the other, but right now it's treating every number like it belongs in all three categories.
How do I get the counts correct? ie., how many numbers fit into the "Less than 1", "Between 1 and 1000", and "Greater than 1000" categories, respectively.
The current code:
$numbers = get_numbers();
$count_less_than_one = 0;
$count_between_one_and_thousand = 0;
$count_greater_than_thousand = 0;
foreach ($numbers as $number) {
$count_less_than_one += 1;
$count_between_one_and_thousand += 1;
$count_greater_than_thousand += 1;
}
Very simply just include the conditions. You can just use if
foreach ($numbers as $number) {
if ($number < 1) $count_less_than_one += 1;
else if ($number >= 1 && $number <= 1000) $count_between_one_and_thousand += 1;
else $count_greater_than_thousand += 1;
}