0

Not sure if my question is clear, but here's what I'm trying to achieve. Let’s say I have a multidimensional array like so:

$arr['client1']**['dog']['Jack']**
$arr['client2']['cat']['Stacy']

How can I get the second portion of the array (between **), knowing it can be anything. For client 3, it could be a crocodile. For Client 4, it could be a car.

So I'm looking to "build" the structure of the array, dynamically. Something like so:

$arr['client1']{partBetweenThe**InTheExemple}

{partBetweenThe**InTheExemple} would be constructed "on the fly" (hence, the dynamically).

EDIT: Hopefully some clarifications...

The array changes every time. Basically, I'm building an addon to poll any API on the web. The data structure I'm getting can be anything. So what I need to do is build the key combination "on the fly", with variables.

In the exemple above, my variable would be something like $query = ['dog']['Jack'] and to get the value, I would poll it like so (from a logistic perspective, I know this doesn't work):

$arr['client1'][$query] or $arr['client1']$query or $arr['client1']{$query}

6
  • Would car be in a variable or what? Check here stackoverflow.com/questions/27929875/… Commented Mar 24, 2022 at 20:44
  • Can you show us a proper/real example of an array, how you would create it and explain what it's for? That would make it much clearer for us to suggest a good solution. Commented Mar 24, 2022 at 20:54
  • You can use array_keys() to get the keys of an array. So array_keys($array['client1'])[0] will be the first key. Commented Mar 24, 2022 at 20:59
  • @M.Eriksson I've clarified above... Commented Mar 24, 2022 at 21:21
  • If the response from the API is dynamic and changes, how would you even know what keys to search for? How would you create the $query to begin with? Commented Mar 24, 2022 at 23:03

2 Answers 2

1

You can define the query as an array with each level as an element. Then we can iterate through that and check if we find a matching key in the response:

function findInArray(array $query, array $data)
{
    foreach ($query as $key) {
        if (!array_key_exists($key, $data)) {
            // The key was not found, abort and return null
            return null;
        }

        // Since the key was found, move to next level
        $data =& $data[$key];
    }
    
    return $data;
}

// Example response
$response = [
    'client1' => [
        'dog' => [
            'Jack' => 'Some value',
        ],
    ]
];

// Define the query as an array
$query = ['dog', 'Jack'];

$result = findInArray($query, $response['client1']);

Demo: https://3v4l.org/WjXTn

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

2 Comments

Your solution seems to be working. Here, I've modified you code with a more complex array: 3v4l.org/m4seY.
Yes, the above should work regardless how many levels you have.
0

Edit:

So since the array's structure can't be changed this will return the client if the structure remains ['client']['animal']['name'].

$clients = [
    'client1' => [
        'dog' => [
            'Jack' => []
        ]
    ],
    'client2' => [
        'cat' => [
            'Stacy' => []
        ]
    ]
];

$animal = 'dog';
$name = 'Jack';

foreach ($clients as $client => $options) {
    if (
        array_key_exists($animal, $options) &&
        array_key_exists($name, $options[$animal])
    ) {
        echo $client;
        break;
    }
}

1 Comment

Unfortunately, the array I'm getting is from an API. I can't change its structure.

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.