如何使用字符串从嵌套数组中获取值

时间:2022-01-06 20:54:04

I have an array like this:

我有一个这样的数组:

$temp = array( '123' => array( '456' => array( '789' => '0' ) ),
               'abc' => array( 'def' => array( 'ghi' => 'jkl' ) )
             );

I have a string like this:

我有这样一条线:

$address = '123_456_789';

Can I get value of $temp['123']['456']['789'] using above array $temp and string $address?

我可以使用上面的$temp数组和字符串$address获取$temp['123']['456']['789']的值吗?

Is there any way to achieve this and is it good practice to use it?

有什么方法可以做到这一点吗?使用它是一种好的做法吗?

2 个解决方案

#1


2  

This is a simple function that accepts an array and a string address where the keys are separated by any defined delimiter. With this approach, we can use a for-loop to iterate to the desired depth of the array, as shown below.

这是一个简单的函数,它接受一个数组和一个字符串地址,其中的键被任何定义的分隔符分隔开。使用这种方法,我们可以使用for循环来迭代数组的期望深度,如下所示。

<?php
function delimitArray($array, $address, $delimiter="_") {
    $address = explode($delimiter, $address);
    $num_args = count($address);

    $val = $array;
    for ( $i = 0; $i < $num_args; $i++ ) {
        // every iteration brings us closer to the truth
        $val = $val[$address[$i]];
        }
    return $val;
    }

$temp = array("123"=>array("456"=>array("789"=>"hello world")));
$address = "123_456_789";
echo delimitArray($temp,$address,"_");
?>

#2


3  

Hello if string $address = '123_456_789'; is your case then you can use explode function to split the string by using some delimeter and you can output your value

您好,如果字符串$address = '123_456_789';那么你的情况是你可以用爆炸函数来分割字符串然后你可以输出你的值吗

<?php
$temp = array('123' => array('456' => array('789' => '0')),
    'abc' => array('def' => array('ghi' => 'jkl')),
);
$address = '123_456_789';
$addr = explode("_", $address);
echo $temp[$addr[0]][$addr[1]][$addr[2]];

#1


2  

This is a simple function that accepts an array and a string address where the keys are separated by any defined delimiter. With this approach, we can use a for-loop to iterate to the desired depth of the array, as shown below.

这是一个简单的函数,它接受一个数组和一个字符串地址,其中的键被任何定义的分隔符分隔开。使用这种方法,我们可以使用for循环来迭代数组的期望深度,如下所示。

<?php
function delimitArray($array, $address, $delimiter="_") {
    $address = explode($delimiter, $address);
    $num_args = count($address);

    $val = $array;
    for ( $i = 0; $i < $num_args; $i++ ) {
        // every iteration brings us closer to the truth
        $val = $val[$address[$i]];
        }
    return $val;
    }

$temp = array("123"=>array("456"=>array("789"=>"hello world")));
$address = "123_456_789";
echo delimitArray($temp,$address,"_");
?>

#2


3  

Hello if string $address = '123_456_789'; is your case then you can use explode function to split the string by using some delimeter and you can output your value

您好,如果字符串$address = '123_456_789';那么你的情况是你可以用爆炸函数来分割字符串然后你可以输出你的值吗

<?php
$temp = array('123' => array('456' => array('789' => '0')),
    'abc' => array('def' => array('ghi' => 'jkl')),
);
$address = '123_456_789';
$addr = explode("_", $address);
echo $temp[$addr[0]][$addr[1]][$addr[2]];