hey guys, i want to find duo array 2 D with same value at least 2
value between $data1 and $data2, and when duo array 2 D founded save in array 3D
then duo array 2 D with same value founded delete
from $data1 and $data2 purpose quick search duo array with same value, how
to use while loop to solve this case?

$data1 = array(
        array(
        '7'=>'chelsea everton villa liverpool',
        '8'=>'everton villa liverpool manutd',
        '9'=>'villa liverpool manutd arsenal',
        '10'=>'liverpool manutd arsenal newcastle'
        ),
        array(
        '23'=>'milan inter juventus napoli',
        '24'=>'inter juventus napoli roma',
        '25'=>'juventus napoli roma udinese',
            ),
        array(
        '53'=>'madrid barcelona altetico getafe',
        '54'=>'barcelona altetico getafe betis',
        '55'=>'altetico getafe betis valencia',
        )
        );




data2 = array(
        array(
        '3'=>'milan inter juventus napoli',
        '4'=>'inter juventus napoli roma',
        '5'=>'juventus napoli roma lazio',
        ),
        array(
        '23'=>'PSG barcelona altetico getafe',
        '24'=>'barcelona monaco getafe betis',
        '25'=>'monaco getafe betis valencia',
        ),
        array(
        '37'=>'swansea watford villa liverpool',
        '38'=>'watford villa liverpool manutd',
        '39'=>'villa liverpool manutd arsenal',
        '40'=>'liverpool manutd arsenal newcastle'
        ),
        array(
        '37'=>'santos flamengo saopaulo riverplate',
        '38'=>'flamengo saopaulo riverplate penarol',
        )
        );


yield>>>
    Array
    (
    [0] = >Array
       (
        [0] => Array
            (
            [7] => 'chelsea everton villa liverpool',
            [8] => 'everton villa liverpool manutd',
            [9] =>'villa liverpool manutd arsenal',
            [10]=>'liverpool manutd arsenal newcastle'        
            )

        [1] => Array
            (
            [37]=>'swansea watford villa liverpool',
            [38]=>'watford villa liverpool manutd',
            [39]=>'villa liverpool manutd arsenal',
            [40]=>'liverpool manutd arsenal newcastle'
            )

    [1] = >Array
       (
        [0] => Array
            (
        [23]=>'milan inter juventus napoli',
        [24]=>'inter juventus napoli roma',
        [25]=>'juventus napoli roma udinese',

            )

        [1] => Array
            (
         [3]=>'milan inter juventus napoli',
         [4]=>'inter juventus napoli roma',
         [5]=>'juventus napoli roma lazio',
            )
    )

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

@Rere_1

hey guys, i want to find duo array 2 D with same value at least 2
value between $data1 and $data2, and when duo array 2 D founded save in array 3D
then duo array 2 D with same value founded delete
from $data1 and $data2 purpose quick search duo array with same value, how
to use while loop to solve this case?

I got no idea what you are trying to do? The past couple of thread you posted are array without a code? My question is why don't used post the actually code to show how you got those arrays?

Here are a couple of links to look at regarding about find array with same value and remove it's from array before .

Try to used this:

http://www.php.net/manual/en/function.array-search.php

and this:

http://www.php.net/manual/en/function.array-replace.php

It would be easier knowing how the main arrays are generated. If the source is a database this can be done in a previous step. And also, as LastMitch pointed, post also your code. Anyway, this is a test example, it outputs the new arrays without the collision:

<?php

$data1 = array(
        array(
        '7'=>'chelsea everton villa liverpool',
        '8'=>'everton villa liverpool manutd',
        '9'=>'villa liverpool manutd arsenal',
        '10'=>'liverpool manutd arsenal newcastle'
        ),
        array(
        '23'=>'milan inter juventus napoli',
        '24'=>'inter juventus napoli roma',
        '25'=>'juventus napoli roma udinese',
            ),
        array(
        '53'=>'madrid barcelona altetico getafe',
        '54'=>'barcelona altetico getafe betis',
        '55'=>'altetico getafe betis valencia',
    '8'=>'everton villa liverpool manutd',
        )
        );

$data2 = array(
        array(
        '3'=>'milan inter juventus napoli',
        '4'=>'inter juventus napoli roma',
        '5'=>'juventus napoli roma lazio',
        ),
        array(
        '23'=>'PSG barcelona altetico getafe',
        '24'=>'barcelona monaco getafe betis',
        '25'=>'monaco getafe betis valencia',
        ),
        array(
        '37'=>'swansea watford villa liverpool',
        '38'=>'watford villa liverpool manutd',
        '39'=>'villa liverpool manutd arsenal',
        '40'=>'liverpool manutd arsenal newcastle'
        ),
        array(
        '37'=>'santos flamengo saopaulo riverplate',
        '38'=>'flamengo saopaulo riverplate penarol',
    '8'=>'everton villa liverpool manutd',
        )
        );



$n = 0;
$matches = array();
$new_data1 = array();
$new_data2 = array();
$n1 = count($data1);
$n2 = count($data2);

for($x = 0; $x < $n1; $x++)
{
    # loop $data1 array
    foreach($data1[$x] as $k1 => $v1)
    {

        for($y = 0; $y < $n2; $y++)
        {

            # loop $data2 array
            foreach($data2[$y] as $k2 => $v2)
            {
                # check if values from first and second are the same
                if($v1 == $v2)
                {

                    # $hash variable is used to save sha1 outputs of the two matching
                    # keys arrays and use them as a compare base in the in the next loops.
                    # The check is done via in_array() function because you wrote that
                    # two matches are enough.

                    $hash = sha1($x.$y);

                    # if there is a match then register the keys of the first dimension
                    # of both arrays
                    if(!empty($remove['sha']) && in_array($hash,$remove['sha']))
                    {
                        $remove['to_remove'][0][] = $x; # for $data1
                        $remove['to_remove'][1][] = $y; # for $data2
                    }

                    # register first matches, this will be executed
                    # before the previous IF condition, because $remove['sha'] it still
                    # doesn't exist.
                    else
                    {
                        # just for checking
                        $remove['a+b'][$n] = '$data1: '.$x.' $data2: '.$y;

                        # list of matches
                        $remove['sha'][$n] = sha1($x.$y);
                    }

                    # all matches
                    $remove['all_matches'][] = $x.' '.$y;
                    $n++;
                }
            }
        }
    }
}

# uncomment below to print the entire $remove array
# print_r($remove);

# list keys to remove
print_r($remove['to_remove']);


# loop $data1 and $data2 to match arrays to remove
foreach($data1 as $k => $v)
{
    if(!in_array($k,$remove['to_remove'][0]))
    {
        $new_data1[] = $v;
    }
}

foreach($data2 as $k => $v)
{
    if(!in_array($k,$remove['to_remove'][1]))
    {
        $new_data2[] = $v;
    }
}


# new arrays
echo 'new $data1' . "\n";
print_r($new_data1);
echo "\n";

echo 'new $data2' . "\n";
print_r($new_data2);

echo "\n";

?>

bye.

commented: Thank You! +6

this array get from prcess before,..

$data1 = array(
        array(
        '7'=>'chelsea everton villa liverpool',
        '8'=>'everton villa liverpool manutd',
        '9'=>'villa liverpool manutd arsenal',
        '10'=>'liverpool manutd arsenal newcastle'
        ),
        array(
        '23'=>'milan inter juventus napoli',
        '24'=>'inter juventus napoli roma',
        '25'=>'juventus napoli roma udinese',
            ),
        array(
        '53'=>'madrid barcelona altetico getafe',
        '54'=>'barcelona altetico getafe betis',
        '55'=>'altetico getafe betis valencia',
        '56'=>'everton villa liverpool manutd',
        )
        );
$data2 = array(
        array(
        '3'=>'milan inter juventus napoli',
        '4'=>'inter juventus napoli roma',
        '5'=>'juventus napoli roma lazio',
        ),
        array(
        '23'=>'PSG barcelona altetico getafe',
        '24'=>'barcelona monaco getafe betis',
        '25'=>'monaco getafe betis valencia',
        ),
        array(
        '37'=>'swansea watford villa liverpool',
        '38'=>'watford villa liverpool manutd',
        '39'=>'villa liverpool manutd arsenal',
        '40'=>'liverpool manutd arsenal newcastle'
        ),
        array(
        '76'=>'santos flamengo saopaulo riverplate',
        '77'=>'flamengo saopaulo riverplate penarol',
        '78'=>'everton villa liverpool manutd',
        )
        );


yiel>>>


[0] = >Array
       (
        [0] => Array
            (
            [7] => chelsea everton villa liverpool',
            [8] => everton villa liverpool manutd',
            [9] => villa liverpool manutd arsenal',
            [10]=> liverpool manutd arsenal newcastle'        
            )
        [1] => Array
            (
            [37]=>'swansea watford villa liverpool',
            [38]=>'watford villa liverpool manutd',
            [39]=>'villa liverpool manutd arsenal',
            [40]=>'liverpool manutd arsenal newcastle'
            )
    [1] = >Array
       (
        [0] => Array
            (
        [23]=>'milan inter juventus napoli',
        [24]=>'inter juventus napoli roma',
        [25]=>'juventus napoli roma udinese',
            )
        [1] => Array
            (
         [3]=>'milan inter juventus napoli',
         [4]=>'inter juventus napoli roma',
         [5]=>'juventus napoli roma lazio',
            )
    )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.