johne90 Posted September 7, 2010 Share Posted September 7, 2010 I am trying to pass a variable of ids into an array, but for some reason its not counting each as an individual instead its lumping all in as one. $data = '"1","1","2","2","3"'; $dirty = array($data); $cleaned = array_unique($dirty); echo count($cleaned)."<br><br>"; foreach($cleaned as $vid){ echo $vid."<br>"; } If I change it to this it works: $dirty = array("1","1","2","2","3"); Any help with this? Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted September 7, 2010 Share Posted September 7, 2010 $data, as you have it defined, is a string, therefore when you assign it to the $dirty array it becomes one element of that array, $dirty[0]. Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108319 Share on other sites More sharing options...
rwwd Posted September 7, 2010 Share Posted September 7, 2010 That's correct syntax when it's working, otherwise you are defining it as a string not an array, to add things to an array, you would need to use the array operand [] So this:- $dirty = array(1,1,2,2,3);//don't need to have the quotes as this is ints in array $cleaned = array_unique($dirty); echo count($cleaned)."<br><br>"; foreach($cleaned as $vid){ echo $vid."<br>"; } is correct, the other version would have all of the numbers assigned to ['0'] position of the array, as a string too. Cheers, Rw Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108320 Share on other sites More sharing options...
johne90 Posted September 7, 2010 Author Share Posted September 7, 2010 Thanks for the info. Is there a way that I can do it though? The $data variable is dynamically generated when the script is run, and so I can't just put them in the code directly. Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108331 Share on other sites More sharing options...
Pikachu2000 Posted September 7, 2010 Share Posted September 7, 2010 Post the code that generates the $data variable . . . Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108333 Share on other sites More sharing options...
johne90 Posted September 7, 2010 Author Share Posted September 7, 2010 I don't see how that is relevant to this. Its a for statement which collects each id and then they are assigned to the $data variable. Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108335 Share on other sites More sharing options...
Pikachu2000 Posted September 7, 2010 Share Posted September 7, 2010 Then all I can offer is that you need to change the process by which the $data variable is put together and then use explode(), or don't change the process, use str_replace() to get rid of the double quotes then use explode(). Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108358 Share on other sites More sharing options...
rwwd Posted September 7, 2010 Share Posted September 7, 2010 Hi all, use str_replace() to get rid of the double quotes then use explode() In the context of this example/question, and without seeing the generation method of the var, that would indeed be the way to proceed. Cheers, Rw Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108364 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2010 Share Posted September 7, 2010 Why aren't you building the array directly from the source data? Processing the same data 3-4 times makes no sense. Link to comment https://forums.phpfreaks.com/topic/212780-array-not-working/#findComment-1108379 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.