deregular

Working on a project here guys and would appreciate some guidence.

Ok so i have two arrays..

eg..

$items[] contains

1
11
2
7
98
etc..

$catalogue[] contains

s1
L11
s2
XL7
m98
etc..

What I am looking to do is compare both,
If $catalogue[] contains a match with one of the items in $items[]
then i need to alter the $items[] array item to the same thats in $catalogue[]..

Humbling myself a little here, i have no idea which array functions or anything to use.

Another problem would be if I am looking for '1' from the $items[] array, Im presuming 2 matches would ap

pear

 . ('s1','L11').

How would I get around this?

Thanks in advance.

webprofessor

/*
This should do what you want...
The $needles are the substrings your searching for and resetting the value on a match
$haystack is the values your checking
returns an array of the new values.

some issues:
- if you were expecting more than one match this will miss it after the swap. You didn't specify which match to choose in a multiple match situation.
- If there is a second match after the wswap the value will get changed again... maybe you don't want this.. maybe you do for some clever compiling trick your doing I don't know. If you don't then you add a break to the inner loop on a match ( I added it in but its commented out )
*/

function swap_on_substring_match( array $needles, array $haystack )
{
      $items_in_haystack = count( $haystack );
      $items_in_needles = count( $needles );

      for( $j = 0; $j < $items_in_needles; $j++)
      {
            for( $i = 0; $i < $items_in_haystack; $i++ )
            {
                        if( is_numeric( subpos(  $haystack[$i], $needles[$j] )
                        {
                                $needles[$j] = $haystack[$i];
//                              break; // we had a swap so we don't want to check this anymore.. see the notes about though
                        }
            }
      }
      return $needles;
}

webprofessor

// A better name for the the function would be "replace_on_substring_match" since there is no swapping going on.

deregular

Thanks WP thats awesome, exactly what i was looking for.

cheers, much appreciated.

d


Perkiset's Place Home   Politics @ Perkiset's