搜索数组键和返回匹配的密钥索引 [英] Search array keys and return the index of matched key

查看:117
本文介绍了搜索数组键和返回匹配的密钥索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组是这样的:

[sx1] => Array
        (
            [sx1] => Pain in Hand
            [sx1L] => Location
            [sx1O] => Other Treat
            [sx1T] => Type
            [sx1R] => Radiation
            [sx1A] => Aggrivate Ease
            [sx1D] => Duration
            [sx1I] => Irit
            [sx1P] => Previous Hx
            [SX1T_1] => CX
            [SX1T_2] => Shld
            [SX1T_3] => Trnk
            [SX1T_4] => Hip
            [SX1T_5] => 
        )

我需要能够通过一键搜索数组,然后返回匹配项的索引。
例如,我需要搜索阵列的关键SX1T_1,然后在数组中返回该项目的索引。

I need to be able to search the array by a key, and then return the index of the matched item. For example, I need to search the array for the key "SX1T_1" and then return the index of that item in the array.

感谢您的帮助。

推荐答案

您可以使用 array_search 中的数组键( array_keys )来获得数字索引:

You can use array_search on the array keys (array_keys) to get the numerical index:

$array = array(
    'sx1' => 'Pain in Hand',
    'sx1L' => 'Location',
    'sx1O' => 'Other Treat',
    'sx1T' => 'Type',
    'sx1R' => 'Radiation',
    'sx1A' => 'Aggrivate Ease',
    'sx1D' => 'Duration',
    'sx1I' => 'Irit',
    'sx1P' => 'Previous Hx',
    'SX1T_1' => 'CX',
    'SX1T_2' => 'Shld',
    'SX1T_3' => 'Trnk',
    'SX1T_4' => 'Hip',
    'SX1T_5' => '',
);
var_dump(array_search('SX1T_1', array_keys($array)));  // int(9)

这篇关于搜索数组键和返回匹配的密钥索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆