I am importing some fields from a cvs and also pulling some fields from a db.
I would like to know if there is a way to compare the values on a character by character of 2 arrays so that the field can be updated with the concatenation but without duplication.
$subject value might be “1948”
$output value could be “1948 the next war”
Ideally I would like to be able to combine these after comparing them but hopefully without duplicating the content in the first variable - $subject.
I found some info on array_filter but I don’t know if it can be used in this case along with array_merge.
If you want to do this automatically on an array of values, then it sounds like maybe you want to do something like
“remove any array element that is a left prefix of any other array element”
You’re probably going to have to do what you said, do a string comparison.
But if you want help with some arrays then it might be best to post some test values, and be explicit about what you want - something people can just copy and paste.
Making us write out (possibly different) test cases each is a barrier to getting definitive answers.
$a = array(
'1948'=>'1948 was not 1984'
, 'and' => 'so on'
);
$b = array(
'1948 gothem story'
, '1948 the next war'
);
What are the keys? Are the keys important or relevant? Is the above a meaningful representation of sample code, if not think deeply about some of the edge cases you might come across - and like I say, be specific about what should the result be.
No, the keys are not important. The actual data is an exported file that is a variable multi-dimensional array.
I am not sure, but I think that the structure of the file has a problem and that is why some of the data is partially duplicated in the sub arrays of sub arrays.
I will have to think about this one because from what I have found on the forum, the comparison that I am thinking about is not that easy.
The point I was trying to make is that I gave you a working solution to the example you posted. I won’t continue asking you for a better example set of input/output or explanation.
I’m with going to reiterate what the others are saying I’m afraid.
As per Cups’ excellent example, you’re going to have to put together a self-contained small code sample for us.
By the sounds of it, post 2 records from each of the potential sources would do. Then post what you would like to be produced given the examples supplied, detailing how you get there.
Sorry for the delay but I had to do some testing and finally put together a short list of records with duplications to understand this better.
From what I see, it looks like the duplication problem can be solved.
Anyway, I am attaching a short list of records to test.
I think that a string comparison from commas going backward with an exact match that leaves only one of the duplicates per record might be a solution.
A problem that I see might be comparing utf-8 for an exact match.