Efficient way to compare strings and get unique value

726 views Asked by At

I have comma separated n numbers of stings contain numbers like 1,2,3,4,5,6 etc and their length can be n. I have to find numbers those are occurring in all string.

Example input:

$str1       = '1,2,3,4,5,6,7,8,9';
$str2       = '0,1,4,5,6,7,10,20,23,34,333,78';
$str3       = '5,4,8,3,1,1,1,5,6';

expected output:

$result     = '1,4,5,6';

I know I can do this by comparing each string but its not that much efficient. Second option is to get shortest string, then check numbers of that string against each string. It will be little efficient then previous one.

All I want to is to get much efficient method to this.

EDIT:

My html where I get the values from:

<form name="cstm_data_form" id="cstm_data_form">
    <div id="dataSet0" onclick="removeCandidate(0)">
        <input type="hidden" name="hidden_ward_name[0]" value="1,2,3,4,5,6,7,8,9,10,11,12,12,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,50,52,53,54,58,59,60,61,62,63,64,65,66">   
    </div>
    <div id="dataSet1" onclick="removeCandidate(1)">

        <input type="hidden" name="hidden_ward_name[1]" value="4,5,6,7,8,9,10,11,12,13,14,15,16,64,65,66">

    </div>
    <div id="dataSet2" onclick="removeCandidate(2)">
        <input type="hidden" name="hidden_ward_name[2]" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,53,54,55,56,57,58,59,60,61,62,63,64,65,66">

    </div>
    <div id="dataSet3" onclick="removeCandidate(3)">
        <input type="hidden" name="hidden_ward_name[3]" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35">

    </div>
</form>
5

There are 5 answers

1
venca On BEST ANSWER

Try

$result = array_unique(array_intersect(explode(',', $str1), explode(',', $str2), explode(',', $str3)));

Edit: Well the point is to explode strings to arrays, then get intersect and finally pick unique values.

0
thomashoareau On
$str1 = array("1","2",...);
$str2 = ...;
$str3 = ...;

$result = array_intersect($str1,$str2,$str3);

That should works. If you want a , do a explode() like the others show to you.

0
Noman On

Convert your string into array , then array_intersect will return common values.

$arr1       = explode(',','1,2,3,4,5,6,7,8,9');
$arr2       = explode(',','0,1,4,5,6,7,10,20,23,34,333,78');
$arr3       = explode(',','5,4,8,3,1,1,1,5,6');
$arrayInterset = array_intersect($arr1, $arr2, $arr3);

echo '<pre>';print_r($arrayInterset);echo '</pre>';

Output :

Array
(
    [0] => 1
    [3] => 4
    [4] => 5
    [5] => 6
)
0
Rizier123 On

This should work for you:

Just loop through all of your variables which you have, explode() them by a comma and put the exploded array into $arr.

After this just call array_intersect(), with call_user_func_array(), which will call the function in this way: array_intersect($arr[0], $arr[1], ...).

And at the end just take all unique values from the array with array_unique().

<?php

    $str1       = '1,2,3,4,5,6,7,8,9';
    $str2       = '0,1,4,5,6,7,10,20,23,34,333,78';
    $str3       = '5,4,8,3,1,1,1,5,6';

    $i = 1;
    while(isset(${"str" . $i})) {
        $arr[] = explode(",", ${"str" . $i});
        $i++;
    }

    $result = array_unique(call_user_func_array("array_intersect", $arr));
    print_r($result);  //As a string: echo implode(",", $result);

?>

output:

Array
(
    [0] => 1
    [3] => 4
    [4] => 5
    [5] => 6
)
0
Travis Hegner On

For something in the context of your html form, as a loop through your values:

foreach($_REQUEST['hidden_ward_name'] as $key=>$string) {
        if($key == 0) {
                $result = array_unique(explode(',', $string));
        } else {
                $result = array_intersect(array_unique(explode(',', $string)), $result);
        }
}

print_r(array_sort($result));