maliary Posted September 18, 2007 Share Posted September 18, 2007 Hi, I've got a bit of a problem with two arrays. arrayA (1,2,3,4,5) arrayB (4,1,3,5,2) I need to match the two arrays without sorting any array. So the values should remain as they are. For every value in arrayA,I have to search through it every value in arrayB. arrayA (1,2,3,4,5) arrayB (4,1,3,5,2) This is part of a bigger system and sorting the two arrays and matching them up is a solution but it won't work well with the rest of the system. Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/ Share on other sites More sharing options...
GingerRobot Posted September 18, 2007 Share Posted September 18, 2007 What are you actually trying to achieve? Are you just trying to check each element of array 1 to see if it exists in array 2? Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350395 Share on other sites More sharing options...
jitesh Posted September 18, 2007 Share Posted September 18, 2007 <?php $ar1 = array(1,2,3,4,5); $ar2 = array(4,1,3,5,2); if(!array_diff($ar1,$ar2)){ echo "All values of ar1 are available in ar2"; }else{ echo "All values of ar1 are not available in ar2"; } ?> Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350396 Share on other sites More sharing options...
maliary Posted September 18, 2007 Author Share Posted September 18, 2007 GingerRobot, The comparison is used thus: where this - if ($row['nr'] == $arl2[$counterup]) - is true, then the name is printed out. $row['nr'] is arrayB and $arl2[$counterup] is arrayA. $row['nr'] is ordered by pip and array elements can be added or removed. $value[$counterup] holds values. If $row['nr'] and $arl2[$counterup] dont match up then wrong names and values appear. $peg = $db-> execute ("SELECT nr,name FROM table_name ORDER BY pip"); while ($row = $peg -> FetchRow()) { if ( $arl2[$counterup]== $row['nr'] ) { echo '<td>'.$row['name'].'</td>'; echo '<td>'.$value[$counterup].'</td>'; } } Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350456 Share on other sites More sharing options...
sasa Posted September 18, 2007 Share Posted September 18, 2007 is it what you want <?php $arrayA = array('sasa', '123', 'asd', 'ppp'); $arrayB = array('asd', 'sasa', 'ddd', '123', 'popo', 'ppp'); foreach ($arrayB as $nr) { echo 'Value: "', $nr; if (count($key = array_keys($arrayA, $nr)) > 0) echo '" exist in arrrayA with key ', $key[0], '<hr />'; else echo '" not exist in arrrayA.<hr />'; } ?> Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350462 Share on other sites More sharing options...
maliary Posted September 19, 2007 Author Share Posted September 19, 2007 Ok, guys this is the situation What is actually required is that:- Each and every individual value in arrayA is searched through all the values in arrayB For instance The array value 1 of arrayA is matched with 4 returns false, then with 1 it returns true because they match. arrayA (1,2,3,4,5) arrayB (4,1,3,5,2) It goes through all. Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350917 Share on other sites More sharing options...
GingerRobot Posted September 19, 2007 Share Posted September 19, 2007 Well, im not entirely sure of the point, but something like this should do: <?php $array1= array(1,2,3,4,5); $array2 = array(4,1,3,5,2); foreach($array1 as $v){ foreach($array2 as $v2){ if ($v==$v2) echo "TRUE $v = $v2 <br />"; else echo "FALSE $v does not equal $v2 <br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350937 Share on other sites More sharing options...
trq Posted September 19, 2007 Share Posted September 19, 2007 <?php $arrayA = array(1,2,3,4,5); $arrayB = array(4,1,3,5,2); foreach ($arrayA as $a) { if (in_array($a,$arrayB) { echo "$a found\n"; } else { echo "$a not found\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/69736-matching-arrays/#findComment-350939 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.