How to check for offset in a loop - php

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.

Related

Undefined offset for $_SESSION variable

I have been trying to create a very simple shopping cart and I'm having a problem with my $_SESSION array. This is for a school project and I'm trying to make it as simple as possible.
The error I'm getting is:
Notice: Undefined index: cart in C:\xampp\htdocs\Final\menu.php on
line 31
Notice: Undefined offset: 5 in C:\xampp\htdocs\Final\menu.php on
line 31
if(isset($_GET['id'])){
$product_id = $_GET['id'];
$_SESSION['cart'][$product_id]++;
print_r($_SESSION);
print "<br>";
print_r($_GET);
}
Once I've added more than one item to a particular product_id, the error goes away. This is the way the tutorial I read explained to add items to the cart. Any suggestions?
Looks like $_SESSION['cart'] does not yet exist. Since it's going to be an array, instantiate it first with:
if(!array_key_exists('cart', $_SESSION)) $_SESSION['cart'] = array();
Since you have not yet assigned anything to $_SESSION['cart'][$product_id], you'll get this type of error when trying to increment it. You may want to try:
$_SESSION['cart'][$product_id] = (array_key_exists($product_id, $_SESSION['cart'])) ? $_SESSION['cart'][$product_id] +1 : 1;
or as an if statement:
if(array_key_exists($product_id, $_SESSION['cart'])) $_SESSION['cart'][$product_id]++;
else $_SESSION['cart'][$product_id] = 1;
When you do $_SESSION['cart'][$product_id]++; you are actually doing:
$_SESSION['cart'][$product_id] = $_SESSION['cart'][$product_id] + 1;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ generates warning if they keys do not exist yet
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ assigns without problems if they keys do not exist yet
The assignment with newly created keys is not the problem, the warning is generated by php trying to get the actual value of $_SESSION['cart'][$product_id].
To solve this you should properly initialize the variable:
$_SESSION['cart'][$product_id] = isset($_SESSION['cart'][$product_id])
? $_SESSION['cart'][$product_id]++
: 1;

Undefined offset warning in 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] ;

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++){
.
.
.

Get a specific line from object (php)

i get some URLS from HTML site by using
foreach($html->find('source') as $video)
if($video->type =='video/mp4') {
echo $video->src. '<br>';
my output looks like :
http://video.csfd.cz/321/321909/130228151/360.mp4
http://video.csfd.cz/321/321909/130228151/720.mp4
http://video.csfd.cz/321/321909/99476124/360.mp4
http://video.csfd.cz/321/321909/99476124/720.mp4
http://video.csfd.cz/321/321909/99476124/1080.mp4
and i have just no idea how to get just one of this links, i dont know much about objects so it looks pretty impossible for me to solve this problem.
What i try:
I was thinking about converting object to array and work with it, which sound pretty easy, problem is that if i use :
$pole = (array)$video;
echo "$pole[0]";
it says:
http://video.csfd.cz/321/321909/130228151/360.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/130228151/720.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/99476124/360.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/99476124/720.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
http://video.csfd.cz/321/321909/99476124/1080.mp4
Notice: Undefined offset: 0 in C:\xampp\htdocs\xampp\ocul\subor.php on line 61
if i use print_r the output is like tons of code , atleast for 1-2x A4 page, soemthing really crazy
So is there a way how to get one specific line from this (prefer to let me chose which one) , or some way to convert this output to array or anything like that?
It is a bad idea to convert objects into arrays. You could basicly store the links in an array while looping. Try this :
$links = array();
foreach ($html->find('source') as $video) {
if ($video->type == 'video/mp4') {
array_push($links, $video->src);
}
}
And then just use the $links array however you want :
echo $links[0];

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