3

This is a WordPress-specific context but my question is more PHP-related...

I'm finding myself in an odd situation where I need to get a value deep inside a multidimensional array, using another array that contains the sequence of keys in the correct order.

So essentially I have an array the looks something like

[0] => 'neighborhood' [1] => 'street' [2] => 'house'

And ultimately what I need is

get_option( $options_id )['neighborhood']['street']['house']

So basically I need to get the value of 'house' from the Options array by using another array to recurse down to it.

I can't do something like

foreach ( $array as $value ) {
    $sequence .= '[' $value ']';
}
get_option( $options_id ) . $sequence;

because that would fill $sequence with the values as a string. I've been trying things with explode() without any luck, but I'm not really a PHP wizard so I'm hoping there's some kind of obvious answer that I just haven't encountered before.

I know it's a weird situation but any help would be much appreciated!

Shoelaced
  • 846
  • 5
  • 27
  • 1
    If every one of your arrays looks like this, you could try: `getoption($options_id)[$array[0]][$array[1]][$array[2]]`, of course, if it's just an example, and you need something more dynamic, you need to heavily refractor this. – kry Aug 14 '18 at 06:52
  • 1
    Possible duplicate of [use strings to access (potentially large) multidimensional arrays](https://stackoverflow.com/questions/7003559/use-strings-to-access-potentially-large-multidimensional-arrays) – CBroe Aug 14 '18 at 07:05

2 Answers2

5

You might want to create some kind of helper function, where you iterate over parts of your path while keeping track of your temporary result:

function deep_array_get(array $path, array $subject) {

   $result = $subject;
   foreach ($path as $p) {
      $result = $result[$p];
   }

   return $result;

}

Example:

$p = ["a", "b", "c"];
$s = [1, 2, "a" => [3, 4, "b" => ["c" => 5]]];
var_dump(deep_array_get($p, $s));

// Result: int(5)

In your exact usecase it would be used something like this:

$path = ['neighborhood', 'street', 'house'];
$subject = get_option($options_id);

$result = deep_array_get($path, $subject);
Smuuf
  • 6,339
  • 3
  • 23
  • 35
  • I like this. I will remember it for other usages. However, I don't see how this helps the OP. I don't think he knows what 'neighborhood', 'street', 'house' are going to be, so he would have to set the path on every iteration. He would be better off just sticking with the key assignments 0,1,2. imho. But none the less I like the idea. Cheers! – Joseph_J Aug 14 '18 at 07:14
  • I don't see how this does _not_ help the OP. It's a universal method to get a value from nested array structure using another array that defines the path to it. It doesn't matter where the path comes from. It may very well be a static `['neighborhood', 'street', 'house']` path, as well as some other path which is generated dynamically. – Smuuf Aug 14 '18 at 08:51
  • 1
    Helps may have been the wrong word. It does do what he needs. I was just looking at it from the point of view that if the array structure does not change then there is no point to dynamically set a path and then feed a function which loops through the path to return the result. When all you have to do is just set the reference in one line of code. If the depth of the path is not known, your solution is the way to go! – Joseph_J Aug 14 '18 at 08:57
  • 1
    Thanks this worked exactly as I need! You're right in that the `$path` array is dynamic and the actual keys would be unknown, as well as how many there are. But this did the job, thanks again. – Shoelaced Aug 14 '18 at 17:34
0

So if array always has the same format then I think the best thing to do would be like kry suggested.

//Example
$array = [0 => 'neighborhood', 1 => 'street', 2 => 'house'];

$options_id = [1,2,'neighborhood'=>[1,2,3,'street'=>[1,2,3,'house'=>[1,2,3,'myhouse']]]];

$result = $options_id[$array[0]][$array[1]][$array[2]];

print_r($result);

This will output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => myhouse
)

Your code.

$array = [0 => 'neighborhood', 1 => 'street', 2 => 'house'];

//I don't know Wordpress. You can try:
$result = get_option($options_id)[$array[0]][$array[1]][$array[2]]; 

//If that does not work.. Use this.
//$data = get_option($options_id);
//$result = $data[$array[0]][$array[1]][$array[2]];

print_r($result);
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
  • I did consider this but unfortunately the array of keys could be of any length, so I can't know how many to plan for. Thanks for the quick reply, though! – Shoelaced Aug 14 '18 at 17:35