/    Sign up×
Community /Pin to ProfileBookmark

array_unique(); not working

Well, actually yes it is working.
I just discovered that I can get it working using
” print_r ” but it will not work with ” echo “.

I haven’t used .. print_r that much and I’m
not sure of the difference other than .. print_r ..
is a function.

If I remember correctly, “C++” has a similar function
and was used along with formatting text,
but I can’t seem to find a reference for the function.

example code:

[code=php]<?php
$names = array(
“1110_nzd_jpy”,
“1110_nzd_jpy”,
“1110_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_nzd_jpy”,
“1107_aud_usd”,
“1107_nzd_jpy”);

print_r( array_values($names));
echo “<br>”;
$names_unique = array();
$names_unique = array_unique($names);
print_r($names_unique);
?>
[/code]

My question would be, why does it
not work with ” echo “?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 10.2010 — How are you using it with echo? If you just do [b]echo $arrayName;[/b], all you will get is something telling you it's an array. To actually see the content, you would have to loop through the array, something like:
[code=php]
$array = array_unique($array);
foreach($array as $value) {
echo "$value<br />n";
}
[/code]


(Sorry if you already know this and your question means something else entirely -- in which case I need more info on what you mean by "not working". ? )
Copy linkTweet thisAlerts:
@anothenauthorNov 10.2010 — I take it back, it's not working,

I've checked and checked for errors,

changed it to print_r...

[code=php]<table><tr><th>Events</th></tr>
<?php
$i = 0;
while($i < count($name_unique))
{ ?><tr><td><?php print_r( $name_unique[$i]); ?></td></tr>
<?php $i++; } ?>

</table>[/code]
Copy linkTweet thisAlerts:
@anothenauthorNov 10.2010 — duh,

I'm so embarrassed

the array_unique does not change the key#
Copy linkTweet thisAlerts:
@anothenauthorNov 10.2010 — thx Nodog,

Now I'm seeing an advantage for using

... foreach() ...

My looping was correct, however the

array_unique leaves the index keys

with the same value as the original

array. therefore my looping

while($i=0;$i<count(array);$i++){}

would not work.

The foreach() would.
Copy linkTweet thisAlerts:
@NogDogNov 10.2010 — Ah...I see what you mean. I almost always use foreach(), in part just for that reason. But if you ever need the indexes to be renumbered, you could do:
[code=php]
$array = array_values(array_unique($array));
[/code]


(And of course you don't want to do that with an associative array, in most cases anyway. ? )
Copy linkTweet thisAlerts:
@anothenauthorNov 10.2010 — Ah...I see what you mean. I almost always use foreach(), in part just for that reason. But if you ever need the indexes to be renumbered, you could do:
[code=php]
$array = array_values(array_unique($array));
[/code]


(And of course you don't want to do that with an associative array, in most cases anyway. ? )[/QUOTE]


thx NogDog,

I actually began searching for a way to do that

but I realized that if I wanted to refer to a specific

index it would mess up my database search,

however, in some instances I might need to

do that.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...