0

I have the following multi-dimentional array:

<?php 
$array = array(
array('first' => 1, 'second' => 1),
array('first' => 1, 'second' => 1),
array('first' => 2, 'second' => 1),
array('first' => 3, 'second' => 1),
array('first' => 3, 'second' => 1),
array('first' => 3, 'second' => 1));

How can I remove the duplicate first values? While preserving the duplicate second values.

After processing the array should be:

array(
array('first' => 1, 'second' => 1),
array('first' => 2, 'second' => 1),
array('first' => 3, 'second' => 1));

See: http://codepad.org/tMh28KMf

2
  • Can you give us an example with keys that should be removed and some that should not? With the expected result of course! Commented Jul 4, 2012 at 14:40
  • I'm a little confused what you're looking for in the output. Do you want $array = array(1,2,3,4,5,6); or $array = array(array('first' => 1), array('first' => 2), array('first' => 3));? Commented Jul 4, 2012 at 14:41

4 Answers 4

1

Code:

$array = array(
    array('first' => 1, 'second' => 1),
    array('first' => 1, 'second' => 1),
    array('first' => 2, 'second' => 1),
    array('first' => 2, 'second' => 1),
    array('first' => 3, 'second' => 1),
    array('first' => 3, 'second' => 1)
);

$temp = array();
$new = array();

foreach($array as $value)
{
    if(!in_array($value['first'],$temp))
    {
        $temp[] = $value['first'];
        $new[] = $value;
    } 
}

Now $new contains the following:

array(
    array('first' => 1, 'second' => 1),
    array('first' => 2, 'second' => 1),
    array('first' => 3, 'second' => 1),
);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but how if i also keys "second" and "third" - in second and third i don't have remove duplicate
0

You'll have to do some foreach loops and reassemble the array yourself. Check out PHP's array_unique() too.

3 Comments

how can i use this for my example?
I'm sorry, I still don't really understand what you need. Do you want to also filter out duplicates in the 'second' keys too? It also might help to know what purpose this code is for. There may be a better way to architect it overall.
Okay, I see you're not wanting to remove duplicates from subsequent keys after 'first'. But... what if you have some 'first' duplicates, but they differ in subsequent keys 'second' or 'third'?
0

This removes the duplicate arrays:

$array = array_map('unserialize', array_unique(array_map('serialize', $array)));

The keys are still the same so you might need to fix them.

2 Comments

i would like remove values only from keys "first" - in my array can be moreover keys, for example "second" and "third" - for these keys can't not remove duplicate
This is a very "inelegant" method.
0

what if the array is like this

$array = array(
    array('first' => 1, 'second' => 1),
    array('first' => 1, 'second' => 1),
    array('first' => 1, 'second' => 12),
    array('first' => 2, 'second' => 1),
    array('first' => 2, 'second' => 1),
    array('first' => 3, 'second' => 1),
    array('first' => 3, 'second' => 1)
);

$temp = array();
$new = array();

You want new array $new contains the following:

array(
    array('first' => 1, 'second' => 1),
    array('first' => 2, 'second' => 1),
    array('first' => 3, 'second' => 1),
);

or

array(
    array('first' => 1, 'second' => 1),
    array('first' => 1, 'second' => 12),
    array('first' => 2, 'second' => 1),
    array('first' => 3, 'second' => 1),
);

?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.