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…



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

What about this, slightly different approach…


$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 = '&#37;s';",
            mysql_real_escape_string($entry[0])
        )
    );
    if(0 !== mysql_num_rows($result)){
        #entry in db
    }else{
        #entry not in db
    }
}

Assuming NAME is unique, but you can alter the SQL accordingly. :slight_smile:


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

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

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