# VERY EASY QUESTION! comparing two array keys?

14 replies to this topic

### #1 bilis_money

bilis_money
• Members
• 621 posts

Posted 22 August 2006 - 01:13 PM

let's say i have two arrays, \$arr_one[] and \$arr_two[].

now the two arrays has the same length and only single dimentional.
how do you compare both arrays keys if they are equal...

please show me the magic codes...

GIGANTIC Thanks to you in advance!

### #2 onlyican

onlyican
• Members
• 921 posts
• LocationHants - UK

Posted 22 August 2006 - 01:56 PM

use count();
\$array_one_size = count(\$arr_one);
\$array_two_size = count(\$arr_two);

if(\$array_one_size == \$array_two_size){

}
Tell me the problem, I will try tell you the solution

### #3 hitman6003

hitman6003
• Members
• 1,807 posts

Posted 22 August 2006 - 01:59 PM

Use array_diff_key (http://www.php.net/array_diff_key)

### #4 onlyican

onlyican
• Members
• 921 posts
• LocationHants - UK

Posted 22 August 2006 - 02:00 PM

That checks the keys, not the size
Tell me the problem, I will try tell you the solution

### #5 trq

trq
• Staff Alumni
• 31,041 posts

Posted 22 August 2006 - 02:02 PM

That checks the keys, not the size

### #6 onlyican

onlyican
• Members
• 921 posts
• LocationHants - UK

Posted 22 August 2006 - 02:06 PM

sorry, I just saw comparing to arrays
Didn't see the keys
Tell me the problem, I will try tell you the solution

### #7 bilis_money

bilis_money
• Members
• 621 posts

Posted 22 August 2006 - 05:49 PM

any neuron tickler? *bumped*
and by the way i want it to compare EACH ARRAY KEYS from 0 upto the last keys.

### #8 trq

trq
• Staff Alumni
• 31,041 posts

Posted 22 August 2006 - 05:57 PM

any neuron tickler? *bumped*

What?

and by the way i want it to compare EACH ARRAY KEYS from 0 upto the last keys.

In what way? Do you want to make sure each key is the same in each array? Do you want a simple true / false upon comparision?

Have you looked at the manuals array section yourself at all?

### #9 bilis_money

bilis_money
• Members
• 621 posts

Posted 22 August 2006 - 06:44 PM

ok, i think i have a mistake here.
what i want is to compare the VALUES of \$arr_one[] into the KEYS of \$arr_two[]...

i'm also trying to figure out this, i know this is easy... just want to see your ways of solving this little problem.

-just hoping and assuming that someone can solve this logic first before i do.

### #10 hitman6003

hitman6003
• Members
• 1,807 posts

Posted 22 August 2006 - 06:46 PM

Loop through the first array and use array_key_exists (http://www.php.net/array_key_exists) to see if the value is a key.

### #11 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,016 posts

Posted 22 August 2006 - 07:49 PM

Do a 2-way check
```<?php
\$a1 = array (1 => 'a', 2 => 'c', 3 => 'c');
\$a2 = array ('a' => 100, 'b' => '200', 'c' => 300);

\$a1 = array_flip(\$a1);
\$tmp1 = array_diff_key(\$a1, \$a2);
\$tmp2 = array_diff_key(\$a2, \$a1);
if (\$tmp1 || \$tmp2)  {
echo "<p>Differences</p>";
foreach (\$tmp1 as \$k=>\$v) echo \$k,'<br>';
foreach (\$tmp2 as \$k=>\$v) echo \$k,'<br>';
}
else echo '<p>No differences</p>';

?>```

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #12 bilis_money

bilis_money
• Members
• 621 posts

Posted 23 August 2006 - 01:07 AM

Thank you very mucn BARAND.
I know that is very cool answer.

But i can't use that array_diff_key() because i'm using PHP ver. 4.4 only.

thanks anyway.

### #13 kenrbnsn

kenrbnsn
• Staff Alumni
• 8,235 posts
• LocationHillsborough, NJ, USA

Posted 23 August 2006 - 03:35 AM

You want to compare the values of array 1 to the keys of array 2.

```<?php
\$arr1 = array('this','is','a','test','one','two','three');
\$arr2 = array('one'=>'x','two'=>'y','three'=>'a','x'=>'test','test'=>'this','a'=>'is','is'=>'a');
\$arr2_keys = array_keys(\$arr2);
\$arr_diff = array_diff(\$arr2_keys, \$arr1);
if (empty(\$arr_diff)) echo 'The keys in array 2 are the same as the values of array 1';
else echo 'These keys in array 2 [<span style="font-weight:bold;color:red">' . implode(',',\$arr_diff) . '</span>] are not values of array 1';
echo '<br>';
\$arr_diff = array_diff(\$arr1, \$arr2_keys);
if (empty(\$arr_diff)) echo 'The values of array 1 are the same as the keys in array 2';
else echo 'These values of array 1 [<span style="font-weight:bold;color:red">' . implode(',',\$arr_diff) . '</span>] are not keys in array 2';?>```

The above shows both compares. Pick the one you want to use.

Ken

### #14 bilis_money

bilis_money
• Members
• 621 posts

Posted 23 August 2006 - 05:09 AM

Thank you very much KEN!

but i have finished formulating my own and tested it and it works very fine. anyway thanks again.

here is my own codes solution,
```for(\$i=0; \$i<\$max_check; \$i++) {
\$ctmp=\$arr1[\$i];
if(in_array("\$ctmp", \$arr2)) {
#echo "\$i: a1(v\$ctmp)==a2(k\$arr2[\$i])<br>";
//store the \$title value of \$_POST['topic']
\$ktmp=\$arr1[\$i];
\$title_tmp=\$_POST['topic'][\$ktmp];
//fetch now into table 'topic'
\$query = mysql_query("SELECT * FROM topics WHERE title='\$title_tmp'") or die("Search Failed<br>".mysql_error());
\$rows = mysql_num_rows(\$query);
\$dataRow = mysql_fetch_array(\$query);
}
}
```
i just used in_array().

### #15 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,016 posts

Posted 23 August 2006 - 06:52 AM

```<?php
\$a1 = array (1 => 'a', 2 => 'c', 3 => 'c');
\$a2 = array ('a' => 100, 'b' => '200', 'c' => 300);

\$a2 = array_flip(\$a2);
\$tmp1 = array_diff(\$a1, \$a2);
\$tmp2 = array_diff(\$a2, \$a1);
if (\$tmp1 || \$tmp2)  {
echo "<p>Differences</p>";
foreach (\$tmp1 as \$v) echo \$v,'<br>';
foreach (\$tmp2 as \$v) echo \$v,'<br>';
}
else echo '<p>No differences</p>';
?>```

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.