4

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?

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

9
  • I don't understand the question. For arrays, yes this works and is normal usage. For strings no. And they are not related. Arrays are structured data, while strings are just a bunch of characters in a row. Commented Oct 21, 2015 at 16:46
  • Of course you could parse out the $address string, get the 3 keys from it and then use them to address the correct value in $temp, but why? Is there a use case for this? Knowing how this will be used could greatly affect the answer. Commented Oct 21, 2015 at 16:53
  • Search here, your question is answered already. Commented Oct 21, 2015 at 16:54
  • does eval() count as good practice though? Commented Oct 21, 2015 at 16:56
  • 2
    Possible duplicate of Access multidimensional array by string with delimiter Commented Oct 21, 2015 at 16:59

3 Answers 3

7

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.

<?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,"_");
?>
Sign up to request clarification or add additional context in comments.

Comments

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

<?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]];

4 Comments

[Editing my previous coment] It actually works and is very simple. Might be 'prettier' if fit inside a function, but anyway +1 :)
Number of elements in $addr may be different. So it will not work in case of 123_456.
@Awan until the explode, everything is fine. If you build a function to verify your array level before applying $addr as parameter, you'll get it to any string you have (in the same pattern)
If the array varies in length then I would take a look at @Typeless' solution
0

Using this array library you can easily get element value by either converting your string to array of keys using explode:

Arr::get($temp, explode('_', $address))

or replacing _ with . to take advantage of dot notation access

Arr::get($temp, str_replace('_', '.', $address))

Another benefit of using this method is that you can set default fallback value to return if element with given keys does not exists in array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.