Jump to content

Array not working


johne90

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.