SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with array_walk

    Hi..guys...jyst need some little help here...kind of stuck here...hope will get some help here...here's the scenario I have some array data retrive from textfile and has to do some schedule task agains dtbase data...so my problem is ...I can't find the solution to store the unmatch dt texfile agains db data store in others array and store the match data textfile in another array..let say's here...

    matchWithDBDTarray = array();-->store the data textfile match with db data
    notMatchWithDBDTarray = array();-->store the data textfile not match with db data

    here's the summary of all my pitfall...

    PHP Code:

    //some dummy data extract from DB
    $dbArrayData = array(array("A001","B"),array("A002","B"),array("A003","B"),array("A004","B"),array("A005","B"),array("A006","B"),array("A007","B"),array("A008","B"),array("A009","B"),array("A010","B"),array("A011","B"),array("A012","B"));


    //some dummy data extract from textfile
    $txtFileData = array(array("A001","B"),array("A002","B"),array("A020","B"),array("A021","B"),array("A022","B"));


    //start looping $txtFileData

    for($i=0;$i<count($txtFileData);$i++){
        
    //using built-in php array fucntions
        
    array_walk($dbArrayData,"matchingWithDBData",$txtFileData[$i]);
    }




    function 
    matchingWithDBData($valueDB,$key,$txtDtArr){
        if(
    $txtDtArr[0] == $valueDB[0]){
            echo 
    " <strong><font color='#FF9900'>MATCH</font></strong> <br/>";
            
    //match data will save in matchWithDBDTarray
        
    }else{
            echo 
    " <strong><font color='#FF0000'>NOT MATCH</font></strong>";
            echo 
    $key." : DB = ".$valueDB[0]." || TXT = ".$txtDtArr[0]."<br/>";
            
    //unmatch data will save in notMatchWithDBDTarray
        
    }
    }


    Hope someone will shoow me or give me some tips ..go on this...because the real db data sometimes it's about 1000..is ther any  others solution to overcome this...really need help..as I struggle to it..tq in advanced.. 

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What about this, slightly different approach...
    PHP Code:
    $data = array(
        array(
            
    'NAME',
            
    'VALUE'
        
    ),
        array(
            
    'NAME',
            
    'VALUE'
        
    ),
        array(
            
    'NAME',
            
    'VALUE'
        
    )
    );

    foreach(
    $data as $entry){
        
    $result mysql_query(
            
    sprintf(
                
    "SELECT name FROM table WHERE name = '%s';",
                
    mysql_real_escape_string($entry[0])
            )
        );
        if(
    !== mysql_num_rows($result)){
            
    #entry in db
        
    }else{
            
    #entry not in db
        
    }

    Assuming NAME is unique, but you can alter the SQL accordingly.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $map $matched $unmatched = array();

    foreach (
    $dbArrayData as $record) {
        
    $map$record[0] ] = 1;
    }

    foreach (
    $txtFileData as $record) {
        if (isset(
    $map$record[0] ])) {
            
    $matched[] = $record;
        } else {
            
    $unmatched[] = $record;
        }

    You could do this in sql as well(using intersection and difference). But, since you said you want to store them in arrays, this is what I present.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the light up guys....

    Frankly speaking Im go with AnthonySterling approach works well now....but thanks to crmalibu too...


    such a great help...

    TQ again

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just something to bear in mind, if the data set contains a lot of records, this approach will involve a lot of round trips to the database.

    You should be able to slim down the process somewhat by asking the database once for a list of all the records that match (or not).
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •