簡體   English   中英

在 PHP 上搜索數組鍵的一部分

[英]Search part of array key on PHP

使用 print_r 打印名為 yesdayArray 的數組時,我有以下輸出:

Array
(
    [project-id] => Array
        (
            [373482] => Array
                (
                    [responsible-ids] => Array
                        (
                            [171812,129938] => Array
                                (
                                    [0] => Array
                                        (
                                            [task-id] => 18055196
                                            [content] => HU-002 
                                            [responsible-ids] => 171812,129938
                                        )

                                )

                            [171812] => Array
                                (
                                    [0] => Array
                                        (
                                            [task-id] => 18055300
                                            [content] => HU-002
                                            [responsible-ids] => 171812
                                        )

                                    [1] => Array
                                        (
                                            [task-id] => 18055307
                                            [content] => HU-002 - BACK 
                                            [responsible-ids] => 171812
                                        )

                                )

                        )

                )

        )

)

我正在遍歷項目 ID(使用變量 $pid),在本示例“373482”的情況下,還使用 ​​$key 遍歷負責 ID。 作為 $key,我正在使用項目的所有可能的負責 ID 值來匹配並做一些事情。

在只有一個負責的情況下效果很好(因為有一個完整的匹配),但如果有更多,就像在“171812,129938”中沒有匹配。

您將如何驗證 $key(171812 或 129938)是否是負責 ID(“171812,129938”)的一部分?

我嘗試將數組鍵轉換為字符串,以便使用內置的 php 搜索函數,如 substr_count 或 strpos。

$needString = $yesterdayArray["project-id"][$pid]["responsible-ids"][$key];

但是當我打印 needString 時,我得到的是“Array”而不是“171812,129938”。

我能做什么?

對鍵調用explode() ,然后使用in_array()檢查$key是否在數組中。

foreach ($yesterdayArray["project-id"] as $pid => $project) {
    foreach ($project["responsible-ids"] as $resp_ids => $tasks) {
        $resp_id_array = explode(',', $resp_id);
        if (in_array($key, $resp_id_array)) {
            // do something
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM