473,246 Members | 1,570 Online

# Re comma separated List - another question

I can do the match perfectly but what i also need to do is create a third
list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck
"Craig Keightley" <do**@spam.me> wrote in message
news:42*********************@news-text.dial.pipex.com...
Ken that functions works a treat
I have modified it to do waht i need it to do by including it in a loop of
other lists and showing if they are a match or not

Many thanks

Craig
"Ken Robinson" <ke******@rbnsn.com> wrote in message

Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they

match???

the comparison is to check that if product a who supplies products

1,2,3,4,5
can be used instead of product b who supplies 3,5 as product a

supplies them

Here's a pure PHP solution I just created:
<?
function comp_csv(\$s1,\$s2)
{
\$tmp1 = explode(',',\$s1);
\$tmp2 = explode(',',\$s2);
\$tmp3 = array_intersect(\$tmp1,\$tmp2);
\$smaller_array = (count(\$tmp1) < count(\$tmp2))?\$tmp1:\$tmp2;
\$smaller_count = count(\$smaller_array);
\$inboth = FALSE;
if (count(\$tmp3) == \$smaller_count)
if (count(array_diff(\$tmp3,\$smaller_array)) == 0) \$inboth =
TRUE;
return(\$inboth);
}

\$csv1 = '1,2,3,4,5';
\$csv2 = '3,5';
\$x = comp_csv(\$csv1,\$csv2);
var_dump(\$x);
?>
The function:
1) creates temporary arrays from the CSV strings
2) gets the intersection between the two arrays, which should have no
more elements than the smaller array. It can have less.
3) if it has the same number of elements as the smaller array, it
checks whether the it's elements are the same as those in the smaller
array. If so, the returned value is set to TRUE

Ken
BTW, followups changed to just comp.lang.php

Jul 17 '05 #1
1 2202

Craig Keightley wrote:
I can do the match perfectly but what i also need to do is create a third list of comma separated values that are in both

eg:

List 1 => 1,2,3,4,5,6,7,8,11
List 2 => 1,3,4,5,6,7,10,23

Therefore

List 3 => 1,3,4,5,6,7

How do I populate this third list - I'm really stuck

The array_intersect() function returns an array of consisting of all
the elements that are in both arrays. The implode() function can be
used to turn that array back into a CSV string.

<?
\$str_1 = '1,2,3,4,5,6,7,8,11';
\$str_2 = '1,3,4,5,6,7,10,23';
\$str_3 =
implode(',',array_intersect(explode(',',\$str_1),ex plode(',',\$str_2)));
echo \$str_3;
?>

Ken

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 7 by: Craig Keightley | last post by: is it possible to compare acomma separated list aginst another eg comma list 1 => 1,2,3,4,5 comma list 2 => 3,5 can you check that 3 is in both, and 5 is in both, therfore they match??? the... 1 by: Not Me | last post by: Hi, I'm sure this is a common problem.. to create a single field from a whole column, where each row would be separated by a comma. I can do this for a specified table, and column.. and I've... 2 by: Pmb | last post by: I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to... 11 by: Craig Keightley | last post by: I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4 11 by: Shawn Odekirk | last post by: Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal... 5 by: Bob | last post by: I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first... 3 by: starman7 | last post by: I'm attempting a query that gathers product data for a particular product id. One of the items is designer(s) which can be more than one. The product table has comma separated id's of the... 3 by: mahe23 | last post by: All, How do One convert a comma separated column from a text file into rows in oracle. I have a scenario where the list of comma separated values changes dynamically. It is like: abc, ttt,... 17 by: mac | last post by: Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE... 0 by: stefan129 | last post by: Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific... 0 by: egorbl4 | last post by: Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ... 1 by: davi5007 | last post by: Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the... 0 by: MeoLessi9 | last post by: I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines".... 0 by: DolphinDB | last post by: The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take... 0 by: DolphinDB | last post by: Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million... 0 by: Aftab Ahmad | last post by: So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =... 0 by: marcoviolo | last post by: Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...