lj11 Posted August 6, 2008 Share Posted August 6, 2008 if I have an array that looks like this: $taken=array(3,6,5,2,4); how do i create an array that has all numbers from 1 to 9 in it that are not any of the values in the $taken array? I guess it impossible, thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/ Share on other sites More sharing options...
matthewhaworth Posted August 6, 2008 Share Posted August 6, 2008 <?php $taken = array(3, 6, 5, 2, 4); $nottaken = array(); for($i=1; $i<10; $i++) { if(!in_array($i, $taken)) { $nottaken[] = $i; } } print_r($nottaken); ?> Something like that. Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-609927 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2008 Share Posted August 6, 2008 You really should look at the different array functions in the manual, especially at the array_diff() function. Ken Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-609933 Share on other sites More sharing options...
matthewhaworth Posted August 6, 2008 Share Posted August 6, 2008 <?php $dataset = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $taken = array(3, 6, 5, 2, 4); $nottaken = array_diff($dataset, $taken); print_r($nottaken); ?> Using what Ken said. Hey, you learn something new everyday when you help people. However, there is a disadvantage to this because the array will have indexes that aren't.. together.. if you will. I.e. when I ran that I got: Array ( [0] => 1 [6] => 7 [7] => 8 [8] => 9 ) Instead of the other way were I got: Array ( [0] => 1 [1] => 7 [2] => 8 [3] => 9 ) Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-609936 Share on other sites More sharing options...
.josh Posted August 6, 2008 Share Posted August 6, 2008 <?php $dataset = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $taken = array(3, 6, 5, 2, 4); $nottaken = array_diff($dataset, $taken); print_r($nottaken); ?> Using what Ken said. Hey, you learn something new everyday when you help people. However, there is a disadvantage to this because the array will have indexes that aren't.. together.. if you will. I.e. when I ran that I got: Array ( [0] => 1 [6] => 7 [7] => 8 [8] => 9 ) Instead of the other way were I got: Array ( [0] => 1 [1] => 7 [2] => 8 [3] => 9 ) array_values ftw Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-609949 Share on other sites More sharing options...
matthewhaworth Posted August 6, 2008 Share Posted August 6, 2008 <?php $dataset = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $taken = array(3, 6, 5, 2, 4); $nottaken = array_diff($dataset, $taken); $nottaken = array_values($nottaken); print_r($nottaken); ?> Does in fact output: Array ( [0] => 1 [1] => 7 [2] => 8 [3] => 9 ) I don't really like that solution though lol, I don't know why. I don't imagine it to be much faster than the first solution because in the background it's doing the same thing, except this one has to run an extra function.. so if anything, it's slower.. but the speed differences would be so small that it doesn't really matter.. pick whichever.. though to be honest, I think the TS has given up on this thread because he/she didn't get a reply within 10 minutes lol. Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-609950 Share on other sites More sharing options...
.josh Posted August 6, 2008 Share Posted August 6, 2008 except that it's less lines of code and more readable... Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-609955 Share on other sites More sharing options...
lj11 Posted August 6, 2008 Author Share Posted August 6, 2008 THANKYOU ALL SO MUCH!! :D ;D Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-610198 Share on other sites More sharing options...
Barand Posted August 6, 2008 Share Posted August 6, 2008 However, there is a disadvantage to this because the array will have indexes that aren't.. together.. if you will. I.e. when I ran that I got: Array ( [0] => 1 [6] => 7 [7] => 8 [8] => 9 ) Instead of the other way were I got: Array ( [0] => 1 [1] => 7 [2] => 8 [3] => 9 ) Before worrying about gaps in the keys you need to establish whether or not it matters. eg you may just want to do echo join (', ', $nottaken); in which case who cares? Quote Link to comment https://forums.phpfreaks.com/topic/118478-php-array-trouble/#findComment-610247 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.