Değişen boyutlarda çok boyutlu bir dizi Kesişen

3 Cevap php

Şöyle çok boyutlu bir dizi var:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

        [1] => Array
            (
                [id] => 1
            )

        [2] => Array
            (
                [id] => 2
            )

        [3] => Array
            (
                [id] => 5
            )

        [4] => Array
            (
                [id] => 4
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 1
            )

        [1] => Array
            (
                [id] => 3
            )

        [2] => Array
            (
                [id] => 4
            )

        [3] => Array
            (
                [id] => 5
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

    )

)

Ben kesen değer (ler) dönmek için bir yol bulmalıyız. Olur Bu durumda,

[id] => 3

) Dizinin uzunluğu farklı olabilir, bu yüzden sadece (Array_Intersect kullanamazsınız.

3 Cevap

Lütfen diziler yalnızca tamsayıları içeriyordu Bu basit olurdu, ama onlar bir başka dizi içerdiği gibi, biraz daha karmaşık alır. Ama bunu yapmak gerekir:

function custom_intersect($arrays) {
    $comp = array_shift($arrays);
    $values = array();

    // The other arrays are compared to the first array:
    // Get all the values from the first array for comparison
    foreach($comp as $k => $v) {
        // Set amount of matches for value to 1.
        $values[$v['id']] = 1;
    }

    // Loop through the other arrays
    foreach($arrays as $array) {
        // Loop through every value in array
        foreach($array as $k => $v) {
            // If the current ID exists in the compare array
            if(isset($values[$v['id']])) {
                // Increase the amount of matches
                $values[$v['id']]++;
            }
        }
    }

    $result = array();

    // The amount of matches for certain value must be
    // equal to the number of arrays passed, that's how
    // we know the value is present in all arrays.
    $n = count($arrays) + 1;
    foreach($values as $k => $v) {
        if($v == $n) {
            // The value was found in all arrays,
            // thus it's in the intersection
            $result[] = $v;
        }
    }
    return $result;
}

Usage:

$arrays = array(
    array(array('id' => 3), array('id' => 1), array('id' => 2), array('id' => 5), array('id' => 4)),
    array(array('id' => 1), array('id' => 3), array('id' => 4), array('id' => 5)),
    array(array('id' => 3))
);

print_r(custom_intersect($arrays));

Result:

Array
(
    [0] => 3
)

Bu fonksiyon mükemmel değil: Bir dizide yinelenen kimlikleri varsa, o iş olmaz. Bu ilk dizi değerleri benzersiz yapmak için biraz daha fazla kod gerektirir, ama bu muhtemelen durumda çalışacaktır.

Sen array_uintersect() özel karşılaştırma işlevi kullanarak dizilerin kesişimini almak için kullanabilirsiniz. Sen onu aramak zorunda call_user_func_array() olsa, ayrı bir argüman olarak her dizi beklediği gibi:

//build list of parameters for array_uintersect()
$params = array_merge($input, array('compare_func'));

$result = call_user_func_array('array_uintersect', $params);

function compare_func($a, $b) {
    return $a['id'] - $b['id'];   
}

(Her zaman 'Dizi' olacaktır) kendi dize gösterimine karşılaştırarak dizileri karşılaştırmak gibi görünüyor, çünkü sadece, array_intersect() call_user_func_array() ile arayamazsın.

Php.net sitesinde (Array_Intersect fonksiyonu) yorumların birinde belirtildiği gibi.

$a = array(1,2,3,4,5,2,6,1);  /* repeated elements --> $a is not a set */
$b = array(0,2,4,6,8,5,7,9,2,1);  /* repeated elements --> $b is not a set */

$ua = array_merge(array_unique($a));  /* now, $a is a set */
$ub = array_merge(array_unique($b));  /* now, $b is a set */

$intersect = array_merge(array_intersect($ua,$ub));

Bu, dizi dönecektir:

Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
    [4] => 6
)