Jump to content

Matching arrays


maliary

Recommended Posts

 

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
Share on other sites

 

 

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.