Page 1 of 1

Remove duplicate values from array.

Posted: Fri Aug 28, 2009 8:13 pm
by lovelf

Code: Select all

$x=1;
$total=20;
while($x<=$total){
$elvalue=${'content'.$x.'_25'};
if(!isset($myarray)){$myarray = array();}
$myarray[$x] = array($elvalue); 
$x++;
}
${'content'.$x.'_25'} is a text string.

I'm trying to remove duplicates from ${'content'.$x.'_25'}, I get to construct an array with the code above but with array_unique it seems I can't make it unique properly because each ${'content'.$x.'_25'} is stored inside as nested arrays of $myarray then I can't use array_unique (?)

How could I, possibly from a loop (?) put in the values of ${'content'.$x.'_25'} in an array, not as nested arrays so that I could use array_unique to make the sort and remove all duplicates.

Thanks

Re: Remove duplicate values from array.

Posted: Fri Aug 28, 2009 8:19 pm
by sousousou
http://zamov.online.fr/EXHTML/PHP/PHP33.html

first hit on google search on "php array remove duplicates"

Re: Remove duplicate values from array.

Posted: Fri Aug 28, 2009 9:25 pm
by lovelf
That example is sorting from one array only, I can't get the values into one array, I'm putting each value as a nested array inside the main array and array_unique for the main array doesn't sort the arrays inside.

I would need to sort the arrays that are inside or create a new array with the values from the nested arrays inside the main array as one array so that I can use array_unique. I can't figure out how to do that.

Thanks.

Re: Remove duplicate values from array.

Posted: Sun Aug 30, 2009 1:47 am
by lovelf
OK, how it's helpful for someone,

$x=1;
$total=20;
while($x<=$total){
$elvalue=${'content'.$x.'_25'};
if(!isset($myarray)){$myarray = array();}
$myarray[$x] = $elvalue; //instead of: array($elvalue);
$x++;
}

then just use array_unique.

Re: Remove duplicate values from array.

Posted: Sun Aug 30, 2009 3:29 pm
by Ollie Saunders
Variable variables are bad news. You never need them. On occasion they can be useful, but when this is so there are usually better things you can do to clean up the code. At best, variable variables are an intermediary step that an experienced programmer might use for cleaning up someone else's legacy code, removing them once they have served their purpose after. Even than they should never be used as a substitute for an array.

Here your bumping up against one of the problems with them — once they've been created it's hard to get hold of all of them and process them collectively.

Change the code that creates ${'content'.$x.'_25'} to use an array instead. For example, change this:

Code: Select all

$x = 1;
${'content'.$x.'_25'} = 'foo';
to this:

Code: Select all

$content_25 = array();
$x = 1;
$content_25[$x] = 'foo';
and then use:

Code: Select all

array_unique($content_25);
to get the value you're after. If this work requires a radical, time-taking, change to your codebase, do it anyway. If you don't know how to use arrays, learn how.

Re: Remove duplicate values from array.

Posted: Sun Feb 24, 2013 7:11 pm
by lovelf
yes, very true, I decided to go with arrays after some time of always using that.
Thanks