bilis_money Posted August 22, 2006 Share Posted August 22, 2006 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! Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 22, 2006 Share Posted August 22, 2006 use count();$array_one_size = count($arr_one);$array_two_size = count($arr_two);if($array_one_size == $array_two_size){} Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 22, 2006 Share Posted August 22, 2006 Use array_diff_key (http://www.php.net/array_diff_key) Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 22, 2006 Share Posted August 22, 2006 That checks the keys, not the size Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2006 Share Posted August 22, 2006 [quote]That checks the keys, not the size[/quote]And that answers the question. Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 22, 2006 Share Posted August 22, 2006 sorry, I just saw comparing to arraysDidn't see the keys Quote Link to comment Share on other sites More sharing options...
bilis_money Posted August 22, 2006 Author Share Posted August 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2006 Share Posted August 22, 2006 [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? Quote Link to comment Share on other sites More sharing options...
bilis_money Posted August 22, 2006 Author Share Posted August 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 22, 2006 Share Posted August 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 22, 2006 Share Posted August 22, 2006 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] Quote Link to comment Share on other sites More sharing options...
bilis_money Posted August 23, 2006 Author Share Posted August 23, 2006 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 23, 2006 Share Posted August 23, 2006 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 Quote Link to comment Share on other sites More sharing options...
bilis_money Posted August 23, 2006 Author Share Posted August 23, 2006 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(). Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2006 Share Posted August 23, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.