1

I have data (more than 10000) from excel, data format looks like,

Example :

Message | Number 

Test 1    021
Test 2    033
Test 3    022
Test 3    022
Test 3    033
Test 2    022
Test 2    021

Input accepted should be

same number & different message
different number & same message
different number & different message

I'm trying to make validation, using array, I tried array_unique, but I can only use 1 type,

$arrayCon[$i]=array("message"=>$message, "number"=>$number);
$realArray=array_unique($arrayCon["number"]);

How do I compare 2 type within 1 array ?

2 Answers 2

1

You need to apply array_unique() on the array itself, specifying the SORT_REGULAR flag to compare each element:

$realArray = array_unique($arrayCon, SORT_REGULAR)
Sign up to request clarification or add additional context in comments.

Comments

0

You can take advantage of JSON and thus make the data row a string, so array_unique is usable:

$source=array();
foreach($database as $row)
{
    $source[]=json_encode(array("message"=>$row[0],"number"=>$row[1]));
}
$unique=array_map(function($str){
    return json_decode($str,true);
},array_unique($source));

Online demo
(I'm behind a proxy right now that seems to mess up with 3v4l.org's result output. If you can't see the result, please let me know)

print_r on my localhost outputs:

Array
(
    [0] => Array
        (
            [message] => Test 1
            [number] => 021
        )

    [1] => Array
        (
            [message] => Test 2
            [number] => 033
        )

    [2] => Array
        (
            [message] => Test 3
            [number] => 022
        )

    [4] => Array
        (
            [message] => Test 3
            [number] => 033
        )

    [5] => Array
        (
            [message] => Test 2
            [number] => 022
        )

    [6] => Array
        (
            [message] => Test 2
            [number] => 021
        )

)

1 Comment

This is completely unnecessary since 5.2.9, because you can use SORT_REGULAR flag on array_unique() to accomplish the same result.

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.