Jump to content

VERY EASY QUESTION! comparing two array keys?


bilis_money

Recommended Posts

[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?
Link to comment
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.

Link to comment
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]
Link to comment
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
Link to comment
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().
Link to comment
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]
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.