Forum Moderators: coopster
Does anyone know of an easy way to match patterns in two different arrays like so...
<?php
// INPUT DATA
$old_array = array("a", "b", "c", "d", "e", "f", "g", "c");
$new_array = array("c", "d", "e", "f", "g", "a", "b", "c");// OUTPUT DATA
// value [corresponding 'array key' of $old_array]
// $new_array starts with 'c'...
// c [7]
// d e f [3] [4] [5]
// g [6]
// a b c [0] [1] [2]
?>
I have searched the net but no luck thus far.
Many thanks for any help...
Are you trying to keep them grouped?
If you are, based on what, because cdefg appears in both too:
$old_array = array("a", "b", "c", "d", "e", "f", "g", "c");
$new_array = array("c", "d", "e", "f", "g", "a", "b", "c");
So, how are you determining the differences in what to match, so you get the results in your example?
It would have to be on the length of the match. So in the example given
$old_array = array([0]=>'a', [1]=>'b', [2]=>'c');
MATCHES
$new_array = array([5]=>'a', [6]=>'b', [7]=>'c'); SO
$old_array = array([7]=>'c');
WOULD HAVE TO MATCH
$new_array = array([0]=>'c');
BECAUSE
$old_array[2] is already in use.
I'm sure there is a clearer way to explain it but hopefully that makes some sense.
Thanks again for any help.
I've looked at it a couple times, and part of it will depend on if the values are all 'single values', because then you could probably do something with implode() & strpos() since the string position of the single character would correspond to the array piece ($original[N]), but if not, then it gets more complicated.
foreach($new_array as $value) {
if( ($key = array_search($value, $old_array)) !== false) {
$tmp_array[$key] = $value;
unset($old_array[$key]);
}
}
print_r($tmp_array);
If $old_array is important to you, you might copy it before you run that as it will consume your old array.
Cheers.
I thought the OP was looking for grouping based on numerical occurrence in the $old_array, and I kept getting about to where you are (okay I can match those and get the position) and trying to think of a built in function to group the numerical order of matches as a single match without sorting, because it throws the order off... If not then I was misunderstanding the question, but I didn't look at it too long, so I very well may have or the solution to the grouping might be easier than I'm thinking of off the top of my head.
Hope it works, and either way it's close and should give some ideas...
<?
## INPUT ##
$old_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
$new_array = array("a", "b", "c", "g", "h", "i", "j", "k", "d", "e", "f");
#
## OUTPUT ##
# OLD KEYS : 6 7 8 9 10
# OLD ARRAY RESULT : g h i j k
# NEW KEYS : 3 4 5 6 7
# NEW ARRAY RESULT : g h i j k
# OLD KEYS : 3 4 5
# OLD ARRAY RESULT : d e f
# NEW KEYS : 8 9 10
# NEW ARRAY RESULT : d e f
# OLD KEYS : 0 1 2
# OLD ARRAY RESULT : a b c
# NEW KEYS : 0 1 2
# NEW ARRAY RESULT : a b c
#
#
$keys_array = fetchKeyCombos($new_array);
uasort($keys_array, "Ascii_Sort");
$old_array_tmp = $old_array;
foreach($keys_array as $n => $keys_row){
$new_array_tmp = $new_array;
foreach ($new_array_tmp as $nn => $newArray){
$keys = explode(" ", $keys_row);
if(count($new_array_tmp) >= count($keys)){
if (findMatch($old_array_tmp, $new_array_tmp, $keys) === true){
$new_array_tmp_keys = array_keys($new_array_tmp);
$new_array_tmp_keys = array_slice($new_array_tmp_keys, 0, count($keys));
foreach($keys as $n => $k){
$old_array_res[] = $old_array[$k];
$new_array_res[] = $new_array[($new_array_tmp_keys[($n)])];
$old_array_tmp[$k] = NULL;
}
echo "OLD KEYS : ".implode(" ", $keys)."<br>\n";
echo "OLD ARRAY RESULT : ".implode(" ", $old_array_res)."<br>\n";
echo "NEW KEYS : ".implode(" ", $new_array_tmp_keys)."<br>\n";
echo "NEW ARRAY RESULT : ".implode(" ", $new_array_res)."<br>\n<br>\n";
unset($old_array_res);
unset($new_array_res);
}
}
unset($new_array_tmp[$nn]);
}
}
# Functions
function findMatch($old_array, $new_array, $keys){
$new_array_keys = array_keys($new_array);
foreach($keys as $i => $key){
if($old_array[$key] != $new_array[($new_array_keys[$i])]){
unset($match_found);
return false;
}
}
unset($match);
return true;
}
function Ascii_Sort($val_1, $val_2){
$retVal = 0;
$firstVal = strlen($val_1);
$secondVal = strlen($val_2);
if($firstVal > $secondVal){
$retVal = -1;
}else if($firstVal < $secondVal){
$retVal = 1;
}
return $retVal;
}
function fetchKeyCombos($new_array){
$count = count($new_array);
$count_range = range(0,$count);
foreach($count_range as $i => $c){
foreach($new_array as $ii => $new_val){
if (($count-$ii) >= $c){
$keys[] = trim(implode(" ", range($c,($count-$ii))));
}
}
}
return $keys;
}
?>
Thanks again for your help.
Thanks both for your help - the following is what i came up with. Its a bit long winded...
My Help? LMAO...
I looked at the question and thought...
'Uh, the answer is: Complicated'
Then looked at it again and got to about where JC was and thought,
'Uh, yeah, that's complicated and I've got a little project of my own for tonight...'
I guess if that's your idea of help, I'm happy to give you as much as you need, any time! LOL.
Glad you got it working though and the 'long-windedness' of the solution makes me think you wanted the results grouped and it was, how do you say, complicated?