gatzke Posted February 20, 2011 Share Posted February 20, 2011 Using the following code: $qwe = array('a','b','c','d','e'); for($i=0; $i<5; $i++) { $asd = rand(0, sizeof($qwe)-1); echo $qwe[$asd]; unset($qwe[$asd]); } gives the following error: Notice: Undefined offset: <offset> in <directory>\<file>.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/228255-array-unset-undefined-offset/ Share on other sites More sharing options...
jcbones Posted February 20, 2011 Share Posted February 20, 2011 This is a simple problem. You are recounting your array, on each loop. So you will never get more than 3 returned values. On top of this, you are removing the array keys, but you aren't re-ordering the keys. So, at times your array keys would look like $qwe[0], $qwe[2], $qwe[4]. Now if the rand() function returned 3, you would get an un-defined index, because the key no longer exist in the array. Simple fix, is to run it through array_values, to re-order the keys. <?php $qwe = array('a','b','c','d','e'); for($i=0; $i<sizeof($qwe); $i++) { $asd = rand(0, sizeof($qwe)-1); $qwe = array_values($qwe); echo $qwe[$asd]; unset($qwe[$asd]); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228255-array-unset-undefined-offset/#findComment-1177051 Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2011 Share Posted February 20, 2011 That makes sense. If it generates the same number twice, the index has already been unset and no longer exists, therefore throws a warning. Quote Link to comment https://forums.phpfreaks.com/topic/228255-array-unset-undefined-offset/#findComment-1177052 Share on other sites More sharing options...
gatzke Posted February 20, 2011 Author Share Posted February 20, 2011 Thank You jcbones, I just came here to answer myself! That's usually how it works for me; search for an answer for 3 hours, then post the question in a help forum, and finally find the solution while waiting for an answer.. Quote Link to comment https://forums.phpfreaks.com/topic/228255-array-unset-undefined-offset/#findComment-1177053 Share on other sites More sharing options...
gatzke Posted February 20, 2011 Author Share Posted February 20, 2011 This is a simple problem. You are recounting your array, on each loop. So you will never get more than 3 returned values. On top of this, you are removing the array keys, but you aren't re-ordering the keys. So, at times your array keys would look like $qwe[0], $qwe[2], $qwe[4]. Now if the rand() function returned 3, you would get an un-defined index, because the key no longer exist in the array. Simple fix, is to run it through array_values, to re-order the keys. <?php $qwe = array('a','b','c','d','e'); for($i=0; $i<sizeof($qwe); $i++) { $asd = rand(0, sizeof($qwe)-1); $qwe = array_values($qwe); echo $qwe[$asd]; unset($qwe[$asd]); } ?> I just noticed you had started to answer my question before I edited my post. I changed sizeof($qwe) to "5" which also gave the error but was fixed by using your suggestion. Leaving sizeof($qwe) in tact results in only 3 letters being printed, so hard coding or copying the original size of $qwe into another variable does the trick: $qwe = array('a','b','c','d','e'); $sizeofqwe = sizeof($qwe); for($i=0; $i<$sizeofqwe; $i++) {... Quote Link to comment https://forums.phpfreaks.com/topic/228255-array-unset-undefined-offset/#findComment-1177056 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.