$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
Related
$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 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.
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.
i have an array of undefined size, for example :
<?php
$array["foo"] = 86 ;
$array["bar"] = 49 ;
$array["matt"] = 96 ;
?>
i don't want to disturb array's internal pointer , but want to get a COPY of second last value of array instead.
I don't know, why you use a map, when in fact you want an ordered list instead, but
$tmp = array_values($array);
echo $tmp[count($tmp) -2];
should do it. With php5.4 this should work either
echo array_values($array)[count($array)-2];
I'm not sure what size your array is planned for, so copying all values into a separate array might not be a good idea.
The following code slices out an array of length 1 just from the second last position and sets $key and $value.
$pair = array_slice($array, -2, 1, true);
$key = key($pair);
$value = current($pair);
PS: Should probably be put into a simple separated function?!
You can do it this way.
$array["foo"] = 86 ;
$array["bar"] = 49 ;
$array["matt"] = 96 ;
$x = count($array);
foreach($array as $row)
{
if($x == 2)
{ $secondLast = $row;}
$x--;
}
echo $secondLast;
Because you are using associative array.
I've seen questions like this, but if I assign a key-value pair to an empty array lik this
$arr[$key] = $value;
I get the php notice
Notice: Undefined index: <key> in <path>
where of course <key> is whatever value was in $key.
How can I assign a value to a new key, without triggering a Notice?
Thanks!
edit: heres your more precise code. This is exactly whats in my code. Does this any difference?
while( list( $k, $sum ) = mysql_fetch_row( $res ) ) {
if( $sum < 0 ) {
$churn[$k] += $sum;
}
}
declare your array before addind the key
$arr = array();
$arr[$key] = $value;
or simply do
$arr = array($key=>$value);
what do you mean [$value]
You're trying to get the value of a brand new array with a given key
Your syntax isnt correct. Just try my quick testcase which wont throw any notice.
<?php
error_reporting(E_ALL);
$array = array();
$key = 'new_key';
$value = 'new_value';
$array[$key] = $value;
echo '<pre>';
var_dump($array);
exit;
?>
The problem layed in
while( list( $k, $sum ) = mysql_fetch_row( $res ) ) {
if( $sum < 0 ) {
$churn[$k] += $sum;
}
}
due to the += operator. The first time, it gets called, churn['whateverWasIn$k'] is not set. The second time is fine though.. So to get rid of the notices, it has to be like:
(!isset($churn[$k])) ? $churn[$k] = $sum : $churn[$k] += $sum;
Realize the missing + in the middle statement. So if this key does not exist by the time, i want to increase it by $sum, declare it and give it the value of $sum, otherwise just add $sum to the current value.
That's all. It doesn't look to pretty in code, but it gets me rid of 200 notices. Which also doesn't look too nice in my view.
Thanks for your help.