The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 17, 2019, 06:22:05 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Comparing arrays with a twist...  (Read 2369 times)
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« on: August 29, 2007, 08:03:07 PM »

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 appear. ('s1','L11').

How would I get around this?

Thanks in advance.
Logged
webprofessor
Rookie
**
Offline Offline

Posts: 15


View Profile
« Reply #1 on: August 30, 2007, 07:30:21 AM »

/*
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;
}
Logged

No links in signatures please
webprofessor
Rookie
**
Offline Offline

Posts: 15


View Profile
« Reply #2 on: August 30, 2007, 10:43:12 AM »

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

No links in signatures please
deregular
Expert
****
Offline Offline

Posts: 172


View Profile
« Reply #3 on: September 01, 2007, 01:41:11 AM »

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

cheers, much appreciated.

d
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!