Why do I receive Undefined offset error? I'm trying to add 10,20,20 for each element in array. Please help. Thanks in advance
<?php
$arr = array("a","b","c");
$counter = 0;
$status = array();
foreach($arr as $a){
$status[$counter] += 10;
$status[$counter] += 20;
$status[$counter] += 20;
echo $status[$counter]."<br>";
$counter ++;
}
?>
Error:
Notice: Undefined offset: 0 in C:\xampp\htdocs\test\index.php on line 6
300
Notice: Undefined offset: 1 in C:\xampp\htdocs\test\index.php on line 6
300
Notice: Undefined offset: 2 in C:\xampp\htdocs\test\index.php on line 6
300
`
you are trying to add 10 in a undefined array element in this line:
$status[$counter] += 10;
try like this:
$arr = array("a","b","c");
$counter = 0;
$status = array();
foreach($arr as $a){
$status[$counter] = 10;//assign first
$status[$counter] += 20; //concate with assigned element
$status[$counter] += 20;
echo $status[$counter]."<br>";
$counter ++;
}
it should not provide any notices.
In your code, $status is an empty array, so when you attempt to add something to an undefined index you will see that notice (only for the first time).
To initialize $status as an array with values 0 based on the number of elements in $arr:
$status = array_fill(0, count($arr), 0);
You're using an 'add and assign' operator.
If you just want to assign a value then
$status[$counter] = 10;
Will work just fine.
However, you're asking PHP to add something to an existing element in your array, but there's no element there yet since you haven't initialised it. Just initialise your array before you start your loop.
Related
I am attempting to sum two arrays and subtract them from each other in PHP. Below is my attempt at it:
function total_price($totals)
{
$sum = 0;
$sub = 0;
foreach ($totals as $total)
{
$sum += $total['total_selling_price'];
$sub += $total['total_buying_price'];
$profit = $sum - $sub;
}
What I'm trying to do is sum up everything in the sales array and subtract it by everything in the costs array (represented by variable $sub) in order to generate profit. However, I'm unable to, and I get the following error:
UNDEFINED VARIABLE: PROFIT.
I've declared a profit variable within the foreach, so can someone tell me why my variable is considered undefined?
Assign $profit = 0 out of loop.
Also validate totals before iterating.
if(is_array($totals)){
foreach($totals as $total ){
// write your code here
}
}
$all = array($stu_quiz_1, $stu_quiz_2, $stu_quiz_3);
$length = count($all);
$low = 10;
$lowest = 0;
for($i = 0; $i<$length; $i++){
if($all($i)<= $low){ // line 34
$lowest = all($i);
}
else{
continue;
}
return $lowest;
}
I am new at php so please help me to find it. I just want to get lowest value from this code. I have three values like $stu_quiz_1 = 20, and so on ...it shows:
Fatal error: Function name must be a string in C:\xampp\install\htdocs\just\quiz_handle.php on line 34
if($all($i)<= $low){ // line 34
$all is not a function so you can't use parentheses. You'll have to use square brackets [] to access the array value.
To simply get highest or lowest values from an array there are some perfectly suited functions built into the core of PHP - namely max and min.
$all=array( 0,1,23,99,34,838 );
$lowest = min( $all );
$highest= max( $all );
echo $lowest,', ', $highest;
/* output: 0, 838 */
Man... it is not $all($i), but $all[$i].
Assuming your function is called all then your if clause should be called like below
if(all($i)<= $low){ // line 34
Note the missing $ from the beginning, thus the name is a string and not a variable.
This line :
$lowest = all($i);
Means that you're calling the function all() with $i as a parameter.
But you $all is actually an array not a function so to access an element of an array you use [].
So you have to change it to :
$lowest = $all[$i];
First:
Change $all($i) to $all[$i] (on line 34)
Second: Change $lowest = all($i); (below the line 34) with $lowest = $all[$i];. In this you were missing a $ sign in front of all and $i was to be kept inside [] because $all is a variable (containing an array).
I have a large number of arrays
arrawithvalues0
arrawithvalues1
arrawithvalues2
arrawithvalues3
......
arrawithvalues999
Therefore I would like to calculate the name of the array to use in my script
I am using this code:
$secondarray = array();
for($iii = 0; $iii < 1000; ++ $iii) {
$array3 = "arrawithvalues" . $iii;
for($II = 0; $II < 10; ++ $II) {
array_push ( $secondarray, $$array3[$II]);
}
}
But get errors like this
Notice: Undefined variable: a in xxxxxxxxx on line xx
Notice: Undefined variable: r in xxxxxxxxx on line xx
Notice: Undefined variable: r in xxxxxxxxx on line xx
Thanks for reading and help
Use braces, like this:
array_push ( $secondarray, {$$array3}[$II]);
Right now, your code is being interpreted as this:
array_push ( $secondarray, ${$array3[$II]});
which is why you are getting those errors.
I have problem with this error script.
private function setCentroidCluster(){
for ($i=0;$i<count($this->centroidCluster);$i++){
$countObj = 0;
$x = array();
for ($j=0;$j<count($this->objek);$j++){
if ($this->objek[$j]->getCluster()==$i){
for ($k=0;$k<count($this->objek[$j]->data);$k++){ // Error
$x[$k] += $this->objek[$j]->data[$k];
The error is:
Notice: Undefined offset: 0
Notice: Undefined offset: 1
The error in line:
$x[$k] += $this->objek[$j]->data[$k];
First:
$x is an empty array. You want to add something at index $k. This is undefined. You need to define at least something there. There's a diff between auto-assigning array values and incrementing an existing array element:
for ($k=0;$k<count($this->objek[$j]->data);$k++){
if ( !isset($x[$k]) )
$x[$k] = 0; // depending on the type of data[$k] !!!
$x[$k] += $this->objek[$j]->data[$k];
}
should do the trick.
And as a recommendation, make yourself familiar with foreach:
foreach ($this->objek as $obj => $dat )
{
if ( $obj->getCluster() == $i )
{
foreach ( $dat as $datelem )
....
etc.
$options = array('health', 'strength', 'agility', 'stamina', 'defence');
$total = array();
foreach ($options as $value)
{
foreach ($objects as $object)
{
$total[$value] += $object->$value;
}
}
var_dump($total);
I have some objects in an array called $objects. It's giving the data to the $total as it should do.
But by some reason it's whining about some undefined offset.
Notice: Undefined index: health in C:\wamp\www\objbattle\index.php on line 32
Line 32 is: $total[$value] += $object->$value;
Why!? And how do I get rid of it?
Because $total['health'] doesn't exist on the first iteration.
It's because you try to increment an uninitialized value. Try initalize them first.
$total = array('health' => 0, 'strength' => 0, 'agility' => 0, 'stamina' => 0, 'defence' => 0);
The key 'health' doesn't exist of the first loop through the for each. As a result you will get the notice.
You can do:
$total[$value] = 0;
Right above the second foreach.
Or, you could do it the lazy way and suppress the NOTICE:
error_reporting(E_ALL ^ ~E_NOTICE);
Why!?
I think because Sets which PHP errors are reported.
I expect because you are trying to increment an un-initialized value...
try assigning on the first round instead, like this:
$total[$value] = $object->$value;
or more simply, initializing the array values...
$total = array();
$total['health'] = 0;
etc...
or do it like #josmith suggests in his answer