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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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