php 获取key的位置,PHP:在多維數組中查找鍵的確切位置 - PHP: Find the exact position of a key in a multidimensional array ...

I have a dynamic multidimensional array with unique keys in all dimensions. (Array keys can be anything, below array is just an example to show that all keys are unique.)

我有一個動態的多維數組,在所有維度都有唯一的鍵。 (數組鍵可以是任何內容,下面的數組只是一個示例,表明所有鍵都是唯一的。)

$data = array(

'0' => array(

'0-0' => array(

'0-0-0' => array(

'0-0-0-0' => 'some value',

'0-0-0-1' => 'some value',

),

'0-0-1' => array(

'0-0-1-0' => 'some value',

'0-0-1-1' => 'some value',

'0-0-1-2' => 'some value',

),

'0-0-2' => array(

'0-0-2-0' => 'some value',

'0-0-2-1' => 'some value',

),

'0-0-3' => array(

'0-0-3-0' => 'some value',

),

),

'0-1' => array(

'0-1-0' => array(

'0-1-0-0' => 'some value',

'0-1-0-1' => 'some value',

),

'0-1-1' => array(

'0-1-1-0' => 'some value',

'0-1-1-1' => 'some value',

),

'0-1-2' => array(

'0-1-2-0' => 'some value',

'0-1-2-1' => 'some value',

),

),

),

'1' => 'some value',

'2' => array(

'2-0' => 'some value',

'2-1' => array(

'2-1-0' => 'some value',

),

),

);

Depth and count of sub-arrays are dynamic. All keys are unique, but they don't have a pattern as the above example.

子陣列的深度和數量是動態的。所有鍵都是唯一的,但它們沒有上述示例中的模式。

I need to find the exact position (with all parents hierarchically) of a given key in this array. For example;

我需要找到這個數組中給定鍵的確切位置(以層次結構的所有父級)。例如;

get_key_position('0-1-2-1', $data); should return array('0', '0-1', '0-1-2', '0-1-2-1') get_key_position('2-1-0', $data); should return array('2', '2-1', '2-1-0') get_key_position('1', $data); should return array('1')

get_key_position('0-1-2-1',$ data);應該返回數組('0','0-1','0-1-2','0-1-2-1')get_key_position('2-1-0',$ data);應該返回數組('2','2-1','2-1-0')get_key_position('1',$ data);應該返回數組('1')

1 个解决方案

#1

1

I didn't make the effort to go and look at the different links cited by comments under your question, so maybe I'm re-inventing the wheel :)

我沒有努力去看你的問題下評論所引用的不同鏈接,所以也許我正在重新發明輪子:)

Anyway, this seems to work in any situation:

無論如何,這似乎適用於任何情況:

function look4key($key, $data, $path = []) {

if (is_array($data)) {

foreach ($data AS $localKey => $value) {

$localKey = (string)$localKey;

$localPath = array_merge($path, [$localKey]);

if ($localKey == $key) {

return $localPath;

}

if ($nestedPath = look4key($key, $value, $localPath)) {

return $nestedPath;

}

}

}

# returns NULL if $key not found

}

Hope it's rather self-explanatory, except this pitfall: (string)$localKey is needed for keys like '0', '1', etc, because of this automatic cast (look at php manual):

希望它是相當不言自明的,除了這個陷阱:(字符串)$ local,因為這樣的自動演員(看看php手冊):'0','1'等鍵需要$ localKey:

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8.

包含有效整數的字符串將強制轉換為整數類型。例如。鍵“8”實際上將存儲在8下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值