Jump to content

Help with Combining Array Values


perezf

Recommended Posts

i have an array that has a few of the same values

I just want it to combine all the same values

how can i do that

Array
(
    [0] => 10
    [1] => 16
    [2] => 17
    [3] => 10
    [4] => 11
    [5] => 12
    [6] => 
)

Link to comment
Share on other sites

if you mean remove duplicate values use array_unique

 

<?php
$old_array = array(10, 16, 17, 10, 11, 12);
print_r($old_array);

$new_array = array_unique($old_array);
print_r($new_array);
?>

 

returns

 

Array
(
 [0] => 10
 [1] => 16
 [2] => 17
 [3] => 10
 [4] => 11
 [5] => 12
)

Array
(
 [0] => 10
 [1] => 16
 [2] => 17
 [3] => 11
 [4] => 12
)

 

One thing to note is that this will only work of duplicate value === duplicate value e.g. an array containing 10 and '10' will see these as different values as one is an integer and the other is a string.

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.