php undefined offset in for loop - php

This has baffled me for quit a while. I want to compare my data with the data coming next to determine when to change the row.
<?php
$seasons; // laravel eloquent model from controller
$i = 0;
$max = count($seasons);
for($i; $i<$max; $i++):
$x = $i+1;
print_r($seasons[$i]); // ok
print_r($seasons[1]); // ok
print_r($seasons[0+1]); // ok
print_r($seasons[$x]); // undefined
print_r($seasons[$i+1]); // undefined
endfor;
?>

<?php
$seasons; // laravel eloquent model from controller
$i = 0;
$max = count($seasons);
for($i; $i<$max; $i++):
$x = $i+1;
print_r($seasons[$i]); // ok
print_r($seasons[1]); // ok
print_r($seasons[0+1]); // ok
if(isset($seasons[$x])){
print_r($seasons[$x]); // undefined
}
endfor;
?>
By the way, $x and $i+1 these both lines are same. because $x = $i + 1; and again you are doing $i + 1.

i over look that $x could be over the array index limit at the last loop, so i put in a check to solve the problem. if someone else have better way of doing it, let me know too.
$i = 0;
$max = count($seasons);
for($i; $i<$max; $i++):
$x = $i+1;
if($x < $max):
if($seasons[$i]->month != $seasons[$x]->month)
// do something
endif
endif;
endfor;

Related

how to display cycles in php added elements to each cycle in php?

I need your help to solve this problem, I am trying to write the necessary code so that the output is as follows:
PHP! PHP!! PHP!!! PHP!!!! PHP!!!!!
<?php
$y = ['!'];
for($i = 1; $i <= 5; $i++)
{
print "PHP!"."$y\t";
if($y+1 <5)
$y="!";
$y+'!';
}
?>
Until the second word, it adds one more exclamation point and in the rest of the repetitions it has the same number, that is two exclamation points.
How can I solve this problem?
Try this:
<?php
for ($i = 0; $i < 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
You can try it:
<?php
for($i = 1; $i <= 5; $i++)
{
echo "PHP";
for($j = 1; $j <= $i; $j++){
echo "!";
}
}
?>
You can also use str_repeat():
<?php
for ($i = 1; $i <= 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
Output will be: PHP!PHP!!PHP!!!PHP!!!!PHP!!!!!

How to loop a multiplication?

I want to find vector s. the formula is described in the picture below :
https://s.id/2UZKT (sorry i can't post the image)
the $bb_baru is an array which will be assigned the value in the table.
I've tried to loop, but the $multiplication_result variable is undefined.
for ($i=0; $i <= $data['jml_alternatif']; $i++) {
for ($i=0; $i <= ($this->input->post('iKriteria')-1); $i++) {
$multiplication_result *= number_format(pow($rangking[$i]['nilai_rangking'], $data['bb_baru'][$i]),4);
}
$data['vektor_s'][] = $multiplication_result;
}
the result of this case according by picture should be :
$vector_s[]=(4.1407,12.2393);
Declare the $multiplication_result variable before the second loop.
for ($i=0; $i <= $data['jml_alternatif']; $i++) {
$multiplication_result = 1;
for ($i=0; $i <= ($this->input->post('iKriteria')-1); $i++) {
$multiplication_result *= number_format(pow($rangking[$i]['nilai_rangking'], $data['bb_baru'][$i]), 4);
}
$data['vektor_s'][] = $multiplication_result;
}

Notice: Undefined offset: 2

I'm new to php and I have done an example in php book. In there I got below notice. How to prevent this notice ?
<?php
require_once('AddingMachine.php');
$arrayofnumbers = array(100,200);
$objectname = new AddingMachine();
$objectname->addNumbers($arrayofnumbers);
?>
and
<?php
Class AddingMachine
{
private $total = 0;
function addNumbers(array $numbers)
{{
for($i=0;$i<=sizeof($numbers);$i++)
{
$this->total = $this->total + $numbers[$i];
}
echo $this->total;
}
}
}
Change your loop from
for($i=0; $i <= sizeof($numbers); $i++)
to
for($i=0; $i < sizeof($numbers); $i++)
Also preferable to use count.
for($i=0; $i < count($numbers); $i++)
The problem is with the <= sizeof($numbers) (which is equal to count($numbers). It will give you the total count of array elements, which is always one more than the maximum index, because arrays begin counting at 0.
Simply replace the <= with < and you'll be fine.

Echo a variable with $i PHP

I am trying to make a dynamic for loop with some database connection.
<?php for ($i = 1; $i <= 8; $i++): ?>
<?php echo $question[0]->option
<?php endfor; ?>
Where the option is in database stored like this: option1, option2, option3 etc..
I have the $i variable which does the counting, but I do not know how I put that into the $question[0]->option variable. Tried $question[0]->option,$i etc, but no luck.
Try this:
<?php
for ($i = 1; $i <= 8; $i++) {
$num = "option".$i;
echo $question[0]->$num;
}
?>
You don't have to open and close the php tags in a block of only php code.
<?php
for ($i = 1; $i <= 8; $i++):
$optionname = "option$i";
echo $question[0]->$optionname
endfor;
?>

Can i do this (Loop in Loop)?

How can i do something like this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$preparadoex[$i];
};
}
But doesnt work, i think this:
for ($i = 1; $i <= $_SESSION['variable'] * $_SESSION['variable2']; $i++) {
for ($i = 1; $i <= $_SESSION['variable2']; $i++) {
$listo[$i] = $preparado1[$i].$preparadoex[$i];
}
}
The script is simply, i have two classes the first have 2 numbers and the two have 3 numbers i should "connect" all numbers (2) in the first class with all the numbers of the second class:
F-S
1-1,
1-2,
1-3,
2-1,
2-2,
2-3
Thanks
You're re-defining $i inside of the second loop, which conflicts with the first loop's counter. Use another variable, like $j:
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}
You just need to use a different variable name to increment the second 'for' loop.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ )
{
for($j = 1; $j <=$_SESSION['variable2']; $j++)
{
$listo[] = $preparado1[$i] . $preparadoex[$j];
}
}
You can have multiple nested loop, not just two. The only thing you have to make sure is that they each use a unique loop counter.
for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['variable2']; $i++ ){
for($j = 1; $j <=$_SESSION['variable2']; $j++){
$listo[$i] = $preparado1[$i] . $preparadoex[$i];
}
}
In your test case your second for should use another variable like $j.

Categories