RyanMinor Posted November 18, 2011 Share Posted November 18, 2011 I have a for loop that I am looping through. When I use var_dump() I get 10 results (which is what I should be getting). However, when I loop through it using a for() loop I am getting an extra 11th value that is blank. What would be causing this? My code is below: $legacy = new Legacy(); // Loop through each film in the shopping cart for ($i = 0; $i < $myShoppingCartInfo['row_count']; $i++) { // Determine if the film is of a legacy series if ($legacy->isLegacyCheckout($myShoppingCartInfo['data']['PRODUCTSKUID'][$i])) { // Declare array to hold all legacy film entity_id values $legacyEntityIds = array(); $legacyEntityIds = $legacy->getLegacyEntityIds($myShoppingCartInfo['data']['PRODUCTSKUID'][$i]); var_dump($legacyEntityIds); // Loop through all legacy video entity_id's for ($i = 0; $i < $legacyEntityIds['data']['ENTITY_ID']; $i++) { // Grant streaming access to each entity id echo $legacyEntityIds['data']['ENTITY_ID'][$i] . '<br />'; // Insert the product into the order items table /*$query = "INSERT INTO tblOrderItems (OrderItemID, ProductSKUID, RentalDate, OrderID) VALUES (##newID##, '".$myShoppingCartInfo['data']['PRODUCTSKUID'][$i]."', ".($myShoppingCartInfo['data']['RENTALDATE'][$i] === NULL ? 'NULL' : "'".$myShoppingCartInfo['data']['RENTALDATE'][$i]."'").", ".$newOrderID.")"; $cartItemInserted = counter_id_insert($query,'OrderItemID'); $curr_user->addNewEntityLicense($legacyEntityIds['data']['ENTITY_ID'][$i]);*/ } } When I view the source code of the output page in a browser, this is what I see: array(5) { ["row_count"]=> int(10) ["col_count"]=> int(1) ["col_names"]=> array(1) { [0]=> string(9) "entity_id" } ["col_types"]=> array(1) { [0]=> string(3) "int" } ["data"]=> array(1) { ["ENTITY_ID"]=> array(10) { [0]=> string(7) "1681799" [1]=> string(7) "1681872" [2]=> string(7) "1681871" [3]=> string(7) "1681870" [4]=> string(7) "1681869" [5]=> string(7) "1681868" [6]=> string(7) "1681867" [7]=> string(7) "1681866" [8]=> string(7) "1681865" [9]=> string(7) "1681864" } } } 1681799<br />1681872<br />1681871<br />1681870<br />1681869<br />1681868<br />1681867<br />1681866<br />1681865<br />1681864<br /><br /> Quote Link to comment https://forums.phpfreaks.com/topic/251384-null-value-at-end-of-for-loop/ Share on other sites More sharing options...
Psycho Posted November 18, 2011 Share Posted November 18, 2011 All I see is an extra '<br>' tag at the end. Are you sure that tag isn't being echo'd to the page in some code after your for() loop? You can easily verify if it is due to an empty/null value magically being produced in your array. Change the line that produces the output to this echo "[{$legacyEntityIds['data']['ENTITY_ID'][$i]}]<br />\n"; If the problem is due to an empty item being added to the array, then your output will look like this (note the [] in the last line): [1681799]<br /> [1681872]<br /> [1681871]<br /> [1681870]<br /> [1681869]<br /> [1681868]<br /> [1681867]<br /> [1681866]<br /> [1681865]<br /> [1681864]<br /> []<br /> However, if that last BR tag is not generated in that loop then the last lines will look like this [1681867]<br /> [1681866]<br /> [1681865]<br /> [1681864]<br /> <br /> Quote Link to comment https://forums.phpfreaks.com/topic/251384-null-value-at-end-of-for-loop/#findComment-1289407 Share on other sites More sharing options...
Psycho Posted November 18, 2011 Share Posted November 18, 2011 OK, I just took a closer inspection of your code and saw this var_dump($legacyEntityIds); // Loop through all legacy video entity_id's for ($i = 0; $i < $legacyEntityIds['data']['ENTITY_ID']; $i++) { You have a for() loop that continues while $i < $legacyEntityIds['data']['ENTITY_ID'] but $legacyEntityIds['data']['ENTITY_ID'] is an array - it is not the length of the array. Beside, you should NOT (normally) be using a for() loop with a dynamic variable for the index to process an array. You should be using a foreach loop. Also, running queries in loops is very poor implementation. Quote Link to comment https://forums.phpfreaks.com/topic/251384-null-value-at-end-of-for-loop/#findComment-1289409 Share on other sites More sharing options...
RyanMinor Posted November 28, 2011 Author Share Posted November 28, 2011 Thank you very much. That was the problem. I used the count of the array and it works. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/251384-null-value-at-end-of-for-loop/#findComment-1291993 Share on other sites More sharing options...
Psycho Posted November 28, 2011 Share Posted November 28, 2011 I used the count of the array and it works. And, why aren't you using a foreach() loop? Quote Link to comment https://forums.phpfreaks.com/topic/251384-null-value-at-end-of-for-loop/#findComment-1291995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.