/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Why is array_unique eliminating some of my variables entirely?

I’m not sure what’s happening here. I am creating an array of ints and then using array_unique() to eliminate any duplicates values. For some reason, ints that were present before I called array_unique() are nowhere to be found afterward… at all. What I’m left with is an empty index where the missing int should be. Here’s the code:

[CODE]
<?php
// Create my array
$myArray = array(10, 15, 18, 77, 1, 15, 18, 77, 1, 15, 34);

// Print out all the contents of my array
for($i=0; $i<count($myArray); $i++)
echo ‘myArray[‘ . $i . ‘] = ‘ . $myArray[$i] . ‘<br>’;

// Create another array of only the unique values from the original array
$myUniqueArray = array_unique($myArray);

// Line break
echo ‘<br>’;

// Print out all the contents of my new array
for($i=0; $i<count($myUniqueArray); $i++)
echo ‘myUniqueArray[‘ . $i . ‘] = ‘ . $myUniqueArray[$i] . ‘<br>’;
?>
[/CODE]

And the result of the above code is this:

[CODE]
myArray[0] = 10
myArray[1] = 15
myArray[2] = 18
myArray[3] = 77
myArray[4] = 1
myArray[5] = 15
myArray[6] = 18
myArray[7] = 77
myArray[8] = 1
myArray[9] = 15
myArray[10] = 34

myUniqueArray[0] = 10
myUniqueArray[1] = 15
myUniqueArray[2] = 18
myUniqueArray[3] = 77
myUniqueArray[4] = 1
myUniqueArray[5] =
[/CODE]

What’s going on here? What happened to int 34?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Feb 06.2009 — Use the foreach statement because array_unique function maintains the keys and since the key for 34 is 10 and your loop never reaches it.


Use:
[code=php]
foreach ($myUniqueArray as $key => $value) {
echo 'myUniqueArray[' . $key . '] = ' . $value . '<br>';
}

[/code]
Copy linkTweet thisAlerts:
@NogDogFeb 06.2009 — It's there, but you are not seeing it because of the way you are outputting the arrays and because array_unique() does not renumber the array keys.
[code=php]
<?php
$myArray = array(10, 15, 18, 77, 1, 15, 18, 77, 1, 15, 34);

foreach ($myArray as $i => $val)
{
echo 'myArray[' . $i . '] = ' . $myArray[$i] . '<br>';
}

$myUniqueArray = array_unique($myArray);

foreach ($myUniqueArray as $i => $val)
{
echo 'myUniqueArray[' . $i . '] = ' . $myUniqueArray[$i] . '<br>';
}

// or just use print_r():
echo "<pre>".print_r($myUniqueArray,1)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@conquesimoauthorFeb 06.2009 — Thanks for the quick replies, skywalker and nogdog -- I understand what is happening now. Originally, I was using a foreach() loop to cycle through each value in the unique array, but as I kept developing my application, I found that a foreach() loop was not sufficient for my purposes; I need to have a unique array of ints with no empty indexes.

I guess I could just use a foreach() that cycles through $myUniqueArray[] and adds each int to yet another array. Or, I could cycle through the original $myArray and only add items to a $finalArray[] that aren't already in it by utilizing array_search(). Or... is there a better way?

Thanks again.
Copy linkTweet thisAlerts:
@MindzaiFeb 06.2009 — [code=php]$myUniqueArray = array_values(array_unique($myArray));[/code]
Copy linkTweet thisAlerts:
@conquesimoauthorFeb 06.2009 — Great, thank you :-)
×

Success!

Help @conquesimo 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.11,
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,
)...