Undefined offset warning in php - php

I am doing this simple thing in php whenever i run the code i got an error
Notice: Undefined offset: 3 in C:\xampp\htdocs\colorconverter.php on line 37
this is the code that generated that error
function colorConverter($color)
{
preg_match_all("/(\d+\.+\d+)/", $color, $rgba);
list($rgba[0], $rgba[1], $rgba[2], $rgba[3]) = $rgba[1] ;
$rgbaValues = array("RED"=>$rgba[0], "GREEN"=>$rgba[1], "BLUE"=>$rgba[2], "ALPHA"=>$rgba[3]);
return $rgbaValues;
}
although it return correct value but why it still show an error

It should be because you do not have $rgba array with 4 elements from the beginning.
Preg match all returns 2 elements 0 and 1 where second (1) is array which I guess is $rgba[1][0], $rgba[1][1], $rgba[1][2] and so on. You are trying to override $rgba[1] with its child elements.
Either declare new array and fill it with 4 empty elements, or not apply array elements in list() there should be variables:
list($rgba1, $rgba2, $rgba3, $rgba4) = $rgba[1] ;

Related

Undefined offset: 1 error coming from this line7

Every time I try to run my code I am getting a 'Notice: Undefined offset: 1 Error
specifically coming from this line:
$acmark= $summary[1][1] += $student[$row][2] / 25;
I am new to PHP and would like to get this working with the least amount of changes possible. I have uploaded the full source code to Pastebin to make for easier viewing.
http://pastebin.com/Ur8u673V
Thanks in advance guys, Luke.
Looking at the code in your paste, you're initialising $summary to an empty array, and never actually adding anything to it before attempting to read the data. It's equivalent to:
$summary = array();
// ...
$acmark = $summary[1][1] += $student[$row][2] / 25; // $summary[1] isn't defined
Agree with George - you're initializing $summary as an array, but the offset starts at 0. Since you're incrementing in a loop, change line 140 to:
$acmark = $summary[][] += $student[$row][2] / 25;
That will eliminate the notice. You can apply the same solution to the other lines with undefined offsets as well to resolve the other notices.
Consider using an associative array(s) with foreach loops so the array keys are more meaningful. Associative arrays are easier to use and support the idea of using self-documenting code
i think u should set value of array first (maybe with 0), because if undefines is meaning that array is null value (null is different with 0)
add this code after $summary = array(); like below
$summary=array();
//additional code
for($sum=0; $sum<6; $sum++)
$summary[$sum][1]=0;
for($row=0; $row<25; $row++){
.
.
.

Uninitialized string offset while building array

I am getting the following error from the method presented below:
Notice: Uninitialized string offset: 5 in /path/to/file.php on line 30 Fatal error: Cannot access empty property in path/to/file.php on line 30
private function parse($xml, $index = '') {
echo count($xml->children()); //outputs 6
$count = 0;
foreach ($xml->children() as $key => $value) {
$this->$key[$count] = array();
$count++;
}
}
Any ideas why if I build an multi-dimensional in this way it results in an error?
If I change the assignment to:
$this->$key = array($count = > array());
This simply re-assigns the property each loop.
Thanks
Rich
Imagine you've got a string:
$string = 'abc`;
Doing substring access (which looks like array) will return you the character:
echo $string[2]; # c
Or you get your error when you're out of the index:
echo $string[3]; # null + warning
So now accessing a member of your object $this dynamically:
$this->$string[2]; # access $this->c
However this one breaks hardly:
$this->$string[3]; # access $this->null (not possible)
This gives you your fatal error of an empty property, a property with no name.
This explain what happens in your code, you have not told what you're trying to do so I hope this information will help you to continue with writing your parse function.
You should try to create the array before filling it.
I.e. $this->key = array();
That is, before looping through the XML elements.

Drupal 7 - Notice: Undefined index: und in include()

I'm getting this error:
Notice: Undefined index: und in include() (line 24 of /home/cliffdwellerproductions/dev.cliffdwellerdigital.com/Dahl/sites/all/themes/basic/templates/node--page2.tpl.php).
the code is:
if ($node->field_body_left !== NULL) :
$text = trim($node->field_body_left['und']['0']['value']);
else:
$text = '';
Please help, as I haven't been able to define the variable...
Alf
Your $node->field_body_left variable is existent but it doesn't have an 'und' element.
It looks like you're attempting to check for an empty field, but you're using $field_body_left!==null which will only be false if the variable is literally null. When a drupal field is present but empty, it's usually equal to array(). Use != instead of !==, and then it will correctly detect both null variables and empty arrays and move on.
--
Extra info: If the variable had a value, its structure would be:
$field_body_left = array(
'und' => array(
0 => array (
'value' => YOURVALUE
)
)
)
But since it doesn't have a value, its structure is:
$field_body_left = array()

How to check for offset in a loop

How can I check for a offset in cake php in a loop?, I have a message saying this...
Notice (8): Undefined offset: 1 [APP\views\cars\car_details.ctp, line 53]
Notice (8): Undefined offset: 2 [APP\views\cars\car_details.ctp, line 53]
Its in a foreach loop and retrieving items like this
$car_ratings['CarRating'][$j]['reccar_num']
Just run a check of array_key_exists() on the element like:
if(array_key_exists($j, $car_ratings['CarRating'])){
// true
}
Check the size of the array (using count()) then don't go over it.
For example:
for ($i = 0; $i < count($car_ratings['CarRating']); ++$i) {
// use the array at $i
}
Alternatively if you don't want to modify the loop, you can use array_key_exists() to determine if the array has a value defined for a particular key.
Of course using foreach would be better here.
If you could provide more context this answer might be better.

Undefined offset error, but offset is not undefined

I'm getting:
Notice: Undefined offset: 0
in my code, however I can print_r the element I am trying to get and its clearly defined.
function get_members($entries_found) {
$members = $entries_found[0]['member'];
...
}
If I print_r($members) I get the expected output, however I'm still getting the Notice.
Any clues?
Do
var_dump($entries_found);
To check that the array does indeed have an offset of zero. Other things you can try would be reseting the array pointer
reset($entries_found);
of checking if it's set first
if (isset($entries_found[0]['member'])) // do things
If all else fails you could just supress the notice with
$members = #$entries_found[0]['member'];
I don't really know what happens with your $entries_found before accessing it from get_members
But i had the same problem. print_r and var_dump showed me, that the index exists but when i tried to access it i got the offset error
In my case i decoded a json string with json_decode without setting the assoc flag.
// Not working
$assocArray = json_decode('{"207":"sdf","210":"sdf"}');
echo $assocArray[207];
// working witht the assoc flag set
$assocArray = json_decode('{"207":"sdf","210":"sdf"}', true);
echo $assocArray[207];
Got my solution from here: Undefined offset while accessing array element which exists

Categories