douti0687 2017-06-10 23:41
浏览 25
已采纳

如果特定值包含在多维数组的子级别中,则返回顶级数组的键

I want to return the first-level key of a multidimensional array based on the value of one of the sub-levels.

For example if I provide '@google.com' as 'Admin Emails' I want to return 'Google'.

My array is below.

$array = array (
    'Google' => array (
            'Allowed Emails' => array ('@google.com','@gmail.com' ),
            'Admin Emails' => 'laz@google.com, kingkong@gmail.com', // This really is supposed to be a String, not an array
            'Company Name'  => 'Alphabet',
            'Memberships' => array ('websites', 'google' ),
),
    'Facebook' => array (
            'Allowed Emails' => array ('@facebook.com','@facebook.co.uk' ),
            'Admin Emails' => 'markmark@facebook.com, shezzer@facebook.com', 
            'Company Name'  => 'Facebook Limited',
            'Memberships' => array ('websites', 'facebook' ),
),
    'Apple' => array (
            'Allowed Emails' => array ('@apple.com','@icloud.com' ),
            'Admin Emails' => 'timtheking@apple.com', 
            'Company Name'  => 'Apple Inc',
            'Memberships' => array ('computers', 'apple' ),
),
        'Dell2' => array (
            'Allowed Emails' => array ('@dell.com' ),
            'Admin Emails' => 'mikerulez1996@dell.com',
            'Company Name'  => 'Dell Computers',
            'Memberships' => array ('computers', 'dell' ),
)
);

I've managed to get a long way using array_filter but the below only works if the variable I've set (in this case 'google.com') is the first variable in the array.

Any thoughts much appreciated.

function setitem ($item){ return $item['Allowed Emails'][0] === '@google.com' ;}

$filteredarray = array_filter($array, setitem);


$toplevelarray = array_keys($filteredarray);

echo $toplevelarray[0];

Also I need some help passing the variable (in this case '@google.com' into the 'setitem' function as this won't be hard coded in. For some reason, I get an error if I try and do it the normal way.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dougang6821 2017-06-10 23:53
    关注

    Nothing particular to do that:

    $result = []; 
    foreach ($array as $k => $v) {
        if ( in_array('@google.com', $v['Allowed Emails']) )
            $result[] = $k; 
    }
    
    print_r($result);
    

    If you already know that the key is unique use a string in place of an array for $result and use break to stop the loop.

    You can do the same with array_filter or array_reduce but it's slower.

    To pass a variable to a function, nothing magic one more time: add a parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么