dqxyh48864 2015-11-05 00:11
浏览 35

用于多维数组的PHP搜索数组

how to get the index of a children array for an array look like this:

Array
(
    [1000] => Array
        (
            [firstName] => Ori
            [lastName] => Smith
            [children] => Array
                (
                    [0] => 1001
                    [1] => 1002
                    [2] => 1003
                    [3] => 1004
                    [4] => 1005
                    [5] => 1006
                    [6] => 1007
                    [7] => 1008
                    [8] => 1009
                    [9] => 1010
                )
        )
)

so if I give 1009 as the search, it should return 1000.

It does not work with this code:

array_search($childrenId, array_column(myArray, 'children'));
  • 写回答

2条回答 默认 最新

  • dpbv85276 2015-11-05 00:42
    关注

    You can use this function found here:

    function getParentStack($child, $stack) {
    foreach ($stack as $k => $v) {
        if (is_array($v)) {
            // If the current element of the array is an array, recurse it and capture the return
            $return = getParentStack($child, $v);
    
            // If the return is an array, stack it and return it
            if (is_array($return)) {
                return array($k => $return);
            }
        } else {
            // Since we are not on an array, compare directly
            if ($v == $child) {
                // And if we match, stack it and return it
                return array($k => $child);
            }
        }
    }
    
    // Return false since there was nothing found
    return false;
    }
    

    I guess you could get the key by key(getParentStack(1009, $myarr)) or modify the function.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端