homepage Welcome to WebmasterWorld Guest from 54.205.241.107
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Match patterns in two different arrays
Match patterns in two different arrays
alfo

5+ Year Member



 
Msg#: 4022751 posted 11:23 pm on Nov 10, 2009 (gmt 0)

Hi

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...

 

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4022751 posted 11:54 pm on Nov 10, 2009 (gmt 0)

I'm not understanding your example...
Do you mean:
// [c] = '7';
// [def] = '3 4 5';
// [g] = '6';
// [abc] = '0 1 2';

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?

alfo

5+ Year Member



 
Msg#: 4022751 posted 12:29 am on Nov 11, 2009 (gmt 0)

hi - Sorry I should have been clearer on that.

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.

alfo

5+ Year Member



 
Msg#: 4022751 posted 12:31 am on Nov 11, 2009 (gmt 0)

Yes sorry i hadn't noticed "c", "d", "e", "f", "g" - you are correct that would be the first match.

alfo

5+ Year Member



 
Msg#: 4022751 posted 1:04 am on Nov 11, 2009 (gmt 0)

sorry for the confusion - the correct output should be..

// CORRECT OUTPUT DATA
// c, d, e, f, g [2] [3] [4] [5] [6]
// a b [0] [1]
// c [7]

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4022751 posted 1:50 am on Nov 11, 2009 (gmt 0)

Yeah, it's actually a complicated question...

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.

JohnCanyon

10+ Year Member



 
Msg#: 4022751 posted 2:00 am on Nov 11, 2009 (gmt 0)

$old_array = array("a", "b", "c", "d", "e", "f", "g", "c");
$new_array = array("c", "d", "e", "f", "g", "a", "b", "c");
$tmp_array = array();

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.

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4022751 posted 2:35 am on Nov 11, 2009 (gmt 0)

If that's all they wanted then it's quite a bit easier than I thought...

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...

alfo

5+ Year Member



 
Msg#: 4022751 posted 7:58 am on Nov 11, 2009 (gmt 0)

Thanks both for your help - the following is what i came up with. Its a bit long winded and its more than probable that there is a better way but it seems to work.


<?
## 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.

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4022751 posted 9:32 am on Nov 11, 2009 (gmt 0)

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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved