0

I am looping through instances of associative arrays (these associative arrays themselves are part of an array).

For each array I want to return a value, based on a key.

Currently I have:

$image_path = array_column($myarray, 'uri');

But of course array_column stores its values in a array, which, considering it's only returning 1 value, is useless to me.

Is there an existing function that will allow me to just get a value based off a supplied key?

Eg:

$image_path = get_keys_value($myarray, 'uri');

Example array. This is a very basic example. The real thing has many levels to it:

$myarray = array
  (
    'instance' => array(
      'type' => 'somedata',
      'content' => somedata',
      'image' => array(
         'name' => 'photo',
         'uri' => 'path/to/file.png'
      ),
    ),
  );

Desired outcome:

$image_path contains 'path/to/file.png' string.

14
  • you means other than uri ? Commented Mar 31, 2017 at 7:00
  • Please share $myarray so that we can better understand Commented Mar 31, 2017 at 7:01
  • Also share the desired output Commented Mar 31, 2017 at 7:01
  • @Swapper no, I want to return the value that is associated with the key 'uri', eg: uri => some/path/to/image.png Commented Mar 31, 2017 at 7:01
  • 1
    $myarray['instance']['image']['uri'] -- why do you need a function to get it? Commented Mar 31, 2017 at 7:12

2 Answers 2

1

Try this,

function array_column_recursive(array $haystack, $needle)
{
    $found = [];
    array_walk_recursive($haystack, function ($value, $key) use (&$found, $needle) {
        if ($key == $needle) {
            $found[] = $value;
        }
    });
    return $found;
}
echo array_column_recursive($myarray, 'uri')[0];

Here is working code.

array_column will work with only 2 level array structure.

Above array will solve your problem.

I hope this will help

Sign up to request clarification or add additional context in comments.

8 Comments

Hmm I just get the error message 'cannot redeclare: array_column_recursive'
you may already have that function, do one thing rename it array_column_recursive1 and replace this name in calling code. and check
Weird, no matter what I name it it just says there's a problem with the line the echo is on as its already 'declared' on the line the function is on
echo array_column_recursive1($myarray, 'uri')[0]; you sure you did this ?
Yep I am. It just doesnt seem to like referring to the same function twice
|
0

I guess you can use array_map.

Ex:

$arr = [
    [
        'root' => [
            'child1' => [
                'child2' => 123
            ]
        ]
    ],
    [
        'root' => [
            'child1' => [
                'child2' => 456
            ]
        ]
    ],
    [
        'root' => [
            'child1' => [
                'child2' => 789
            ]
        ]
    ],
    [
        'root' => [
            'child1' => [
                'child2' => 123
            ]
        ]
    ],
];

print_r(array_map(function($row) {
    // here goes expression to get required path
    return $row['root']['child1']['child2'];
}, $arr));

Output:

Array
(
    [0] => 123
    [1] => 456
    [2] => 789
    [3] => 123
)

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.