Notice: Undefined offset: 2 - php

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.

Related

String sort in alphabetic order in php8

I am trying to find first element(alphabetical order) from array string using loop without inbuilt functions in php8. My code is below but it is not working a intented. What is the error in my code?
eg: string = "This","is","my","apple";
apple need to print
$arraystring= array("This","is","my","apple");
$count = count($arraystring);
for ($i = 0; $i < $count; $i++) {
for ($j = 1; $j < $count; $j++) {
if(strcmp($arraystring[$i], $arraystring[$j])<0){
$temp = $arraystring[$i];
$arraystring[$i] = $arraystring[$j];
$arraystring[$j] = $temp;
}
}
}
my code not worked as intented

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

For loop (php) that results in this: 12345678910987654321

My niece is trying to create one for-loop (php), that results in this:
* 12345678910987654321
example for loop she tried:
for ($i = 1; $i <= 10; $i++ , $i = 10; $i <= 1; $i--) {
echo $i . ' ';
}
She can only use if's and elseif's. I'm not a programmer and can't really help her. Any ideas how this could be achieved in php?
Any information would be greatly appreciated.
The key is to add a variable instead of a number, then reverse that number when $i hits 10.
for($i = 1, $j = 1; $i> 0; $i+=$j) // Start i at 1, and j at 1
{
echo $i;
if($i == 10)
$j = -1; // i has hit 10, so use -1 to start subtracting
}
Another possibility is to loop up to 20, printing $i for the ascending part and 20 - $i for the descending.
for ($i = 1; $i < 20; $i++) {
if ($i <= 10) {
echo $i;
} else {
echo 20 - $i;
}
}

php undefined offset in for loop

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;

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