Jaret Posted August 9, 2008 Share Posted August 9, 2008 Hello! First post. I want to compare two Arrays, each of which contains more Arrays. Here is some of them. They have ~20 arrays each but I cut them short for my post: First: Array ( [0] => Array ( [0] => 2 [x] => 2 [1] => 4 [y] => 4 ) [1] => Array ( [0] => 2 [x] => 2 [1] => 4 [y] => 4 ) [2] => Array ( [0] => 2 [x] => 2 [1] => 5 [y] => 5 ) [3] => Array ( [0] => 2 [x] => 2 [1] => 6 [y] => 6 ) ) Second: Array ( [0] => Array ( [0] => 3 [x] => 3 [1] => 5 [y] => 5 ) [1] => Array ( [0] => 2 [x] => 2 [1] => 6 [y] => 6 ) ) I want the result to be the first Array without the arrays inside it that are included in the second one. I'm trying to do this with while(), $n and Array_Diff() but I don't have much success so far. Link to comment https://forums.phpfreaks.com/topic/118901-solved-array_diff-have-to-find-a-more-complicated-way-to-use-it/ Share on other sites More sharing options...
Jaret Posted August 9, 2008 Author Share Posted August 9, 2008 I may also change approach. The Arrays are a result of a MySQL Query. So I decided to use an advanced mysql query instead of comparing the results of 2 separate queries. I still need help though! I need to make a query from multiple tables and with multiple conditions. I need to find all ROWS from the table WORLD where X is between (P - 1 and P + 1) and Y is between (P -1 and P + 1) . Then find all ROWS from the table CHARACTERS where X is between (P - 1 and P + 1) and Y is between (P -1 and P + 1) . Then exclude the ROWS from CHARACTERS (if any) from the ROWS of WORLD. P.S. The use of this script is Movement Shadow for a graphical isometric tile based game engine. Movement shadow should make the tiles around the character darker, but not those on which a character is standing. Link to comment https://forums.phpfreaks.com/topic/118901-solved-array_diff-have-to-find-a-more-complicated-way-to-use-it/#findComment-612306 Share on other sites More sharing options...
Jaret Posted August 9, 2008 Author Share Posted August 9, 2008 This how far I got. I have 2 queries that I need to make one big query. Or compare the results from each somehow to get the final result that I need. $range_q = mysql_query("SELECT x , y FROM world WHERE x BETWEEN $char[x] - $range and $char[x] + $range and y BETWEEN $char[y] - $range and $char[y] + $range "); $exclude = mysql_query("SELECT x , y FROM characters WHERE x BETWEEN $char[x] - $range and $char[x] + $range and y BETWEEN $char[y] - $range and $char[y] + $range "); I tried to use this model but can't get it right: SELECT column_list FROM table_1 [iNNER | LEFT | RIGHT] table_2 ON conditions_2 [iNNER | LEFT | RIGHT] table_3 ON conditions_3 ... WHERE conditions Link to comment https://forums.phpfreaks.com/topic/118901-solved-array_diff-have-to-find-a-more-complicated-way-to-use-it/#findComment-612312 Share on other sites More sharing options...
Barand Posted August 9, 2008 Share Posted August 9, 2008 try <?php $a = Array ( Array ( '0' => 2, 'x' => 2, '1' => 4, 'y' => 4 ), Array ( '0' => 2, 'x' => 2, '1' => 4, 'y' => 4 ), Array ( '0' => 2, 'x' => 2, '1' => 5, 'y' => 5 ), Array ( '0' => 2, 'x' => 2, '1' => 6, 'y' => 6 ) ); $b = Array ( Array ( '0' => 3, 'x' => 3, '1' => 5, 'y' => 5 ), Array ( '0' => 2, 'x' => 2, '1' => 6, 'y' => 6 ) ); function my_array_diff($a, $b) { foreach ($a as $k=>$v) { if (in_array($v, $b)) unset($a[$k]); } return $a; } $c = my_array_diff($a, $b); echo '<pre>', print_r($c, true), '</pre>'; Link to comment https://forums.phpfreaks.com/topic/118901-solved-array_diff-have-to-find-a-more-complicated-way-to-use-it/#findComment-612314 Share on other sites More sharing options...
Jaret Posted August 9, 2008 Author Share Posted August 9, 2008 Barand you're a genius! Thank god you're not a criminal mastermind or we would need superman to save us! ...or aren't you?... Link to comment https://forums.phpfreaks.com/topic/118901-solved-array_diff-have-to-find-a-more-complicated-way-to-use-it/#findComment-612333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.