Jump to content

php array trouble


lj11

Recommended Posts

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

Link to comment
Share on other sites

<?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 ) 

 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.