Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

VERY EASY QUESTION! comparing two array keys?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
use count();
$array_one_size = count($arr_one);
$array_two_size = count($arr_two);

if($array_one_size == $array_two_size){

}

Share this post


Link to post
Share on other sites
[quote]That checks the keys, not the size[/quote]

And that answers the question.

Share this post


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

thanks again in advance.

Share this post


Link to post
Share on other sites
[quote]any neuron tickler? *bumped*[/quote]

What?

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

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Do a 2-way check
[code]
<?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>';

?>[/code]

Share this post


Link to post
Share on other sites
Thank you very mucn [b]BARAND[/b].
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.

Share this post


Link to post
Share on other sites
You want to compare the values of array 1 to the keys of array 2.

[code]<?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';?>[/code]

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

Ken

Share this post


Link to post
Share on other sites
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,
[code]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);
}
}
[/code]
i just used in_array().

Share this post


Link to post
Share on other sites
Flip $a2 instead of $a1 and compare values instead of keys:

[code]<?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>';
?>[/code]

Share this post


Link to post
Share on other sites

×

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.