2

I have an array of arrays like this:

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 1
            [4] => 2
            [5] => 2
            [6] => 1
            [7] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [3] => School1
            [4] => School2
            [5] => School2
            [6] => School1
            [7] => 
        )
)

I want to remove all duplicates of each array. So, I want the final array to look like this:

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [7] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [7] => 
        )
)

I've tried this (as recommended in this answer https://stackoverflow.com/a/307701/1009116):

  function multi_unique($updates) {
    foreach ($updates as $k=>$na)
        $new[$k] = serialize($na);
    $uniq = array_unique($new);
    foreach($uniq as $k=>$ser)
        $new1[$k] = unserialize($ser);
    return ($new1);
}

And it has no effect

I also tried this (as recommended here - https://stackoverflow.com/a/946300/1009116)

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

And this just returns the last array (however, it is filtered as it should be)

What am I doing wrong???

2
  • So which array definitively identifies uniqueness? Is it the fieldID array, the educationTitle array, or both? Do you need to maintain the numerical index? Commented Mar 29, 2013 at 0:07
  • fieldId and yes,I need the numerical index Commented Mar 29, 2013 at 0:10

3 Answers 3

3

You can use :

$data = array(
        'userId' => 35,
        'fieldId' => array(
                0 => 1,
                1 => 2,
                2 => 3,
                3 => 1,
                4 => 2,
                5 => 2,
                6 => 1,
                7 => 4
        ),
        'educationTitle' => array(
                0 => 'School1',
                1 => 'School2',
                2 => 3,
                3 => 'School1',
                4 => 'School2',
                5 => 'School2',
                6 => 'School1',
                7 => NULL
        )
);

print_r(arrayUnique($data));

Output

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [7] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [7] => 
        )

)

Function Used

function arrayUnique($array) {
    $input = array_map("unserialize", array_unique(array_map("serialize", $array)));
    foreach ( $input as $key => $value ) {
        is_array($value) and $input[$key] = arrayUnique($value);
    }
    return $input;
}
Sign up to request clarification or add additional context in comments.

Comments

3

You could use array_combine() in this case; later you pull apart the data into their respective containers.

$combined = array_combine($arr['fieldId'], $arr['educationTitle']);

$arr['fieldId'] = array_keys($combined);
$arr['educationTitle'] = array_values($combined);

Do note that the original indices are renumbered after this operation.

Comments

0

Another way using array_flip();

<?php
    $myArray = array( "userID"=>'35',
    "fieldID" => array(
    "0" => '1',"1" => '2',
    "2" => '3',"3" => '1',
    "4" => '2',"5" => '2',
    "6" => '1',"7" => '4'),
    "educationTitle"=>array(
    "0"=>'School1',"1"=>'School2',
    "2"=>'3',"3"=>'School1',
    "4"=>'School2',"5"=>'School2',
    "6"=>'School1',"7"=>'',),);

    print_r($myArray);

    $myArray['fieldID'] = array_flip($myArray['fieldID']);
    $myArray['fieldID'] = array_flip($myArray['fieldID']);
    $myArray['educationTitle'] = array_flip($myArray['educationTitle']);
    $myArray['educationTitle'] = array_flip($myArray['educationTitle']);

    print_r($myArray);
?>

Final Output

Array
(
    [userID] => 35
    [fieldID] => Array
        (
            [6] => 1
            [5] => 2
            [2] => 3
            [7] => 4
        )

    [educationTitle] => Array
        (
            [6] => School1
            [5] => School2
            [2] => 3
            [7] => 
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.