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>
Try
Edit: Well the point is to explode strings to arrays, then get intersect and finally pick unique values.