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
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
Share on other sites

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