Jump to content

[SOLVED] Array_diff() - have to find a more complicated way to use it.


Jaret

Recommended Posts

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.

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.

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

 

 

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>';

Archived

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

×
×
  • 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.