久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

不同大小的相交多維數組

Intersecting multidimensional array of varying size(不同大小的相交多維數組)
本文介紹了不同大小的相交多維數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個如下所示的多維數組:

I have a multidimensional array that looks like this:

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
            )

    )

)

我需要找到一種返回相交值的方法.在這種情況下,這將是

I need to find a way to return the intersecting value(s). In this case that would be

[id] => 3

數組的長度可能不同,所以我不能只使用array_intersect().

The length of the array could be different, so I can't just use array_intersect().

推薦答案

如果你的數組只包含整數,這會很簡單,但是當它們包含另一個數組時,它會變得有點復雜.但這應該可以:

This would be simple if your arrays contained only integers, but as they contain an another array, it gets a bit more complicated. But this should do it:

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

用法:

$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));

結果:

Array
(
    [0] => 3
)

此功能并不完美:如果您在一個數組中有重復的 ID,它將無法工作.這將需要更多的代碼來首先使數組值唯一,但這可能適用于您的情況.

This function isn't perfect: if you have duplicate ID's in one array, it will not work. That would require a bit more code to first make the array values unique, but this will probably work in your case.

這篇關于不同大小的相交多維數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 欧美日高清 | 免费久久99精品国产婷婷六月 | 精精国产xxxx视频在线播放 | 国产91久久久久蜜臀青青天草二 | 一级黄色淫片 | 国产精品久久久久久久久久久久冷 | 亚洲视频精品 | 欧美激情国产精品 | 国产精品一区二区久久久久 | 天天操天天插天天干 | 久久久久国产 | 精品一区二区三区在线观看 | 永久av| 色狠狠一区 | 亚洲精品乱码久久久久久久久 | 精品久久九 | 9久9久9久女女女九九九一九 | 午夜精品久久久久久久99黑人 | 天天av综合 | 亚洲区一区二 | 亚洲综合在线网 | 亚洲综合久久网 | 精品毛片 | 91视频一区 | 亚洲毛片 | 免费观看日韩av | 黄色片视频网站 | 一级黄色片在线看 | 日韩精品一区二区三区 | 久久这里只有精品首页 | 毛片网站在线观看视频 | 亚洲看片 | 欧美一区二区在线播放 | 中文字幕一区二区三区日韩精品 | 国产一级黄色网 | 国产1区2区3区 | 日韩在线一区二区三区 | 亚洲精品日韩一区二区电影 | 国产精品一区二区在线 | 日韩精品在线免费观看 | 亚洲免费精品 |