Is there a way to dynamically access multidimensional PHP array? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, February 28, 2022

Is there a way to dynamically access multidimensional PHP array?

Is there a way to access a PHP multidimensional array (specific index) dynamically?

So for example, say I want to access a value at:

$array[1]['children'][0]['children'][2]['settings']['price']

Can there be a function which returns this value by just getting index (1, 0 and 2 respectively)? and in a way that it works whether there are 3 values (1, 0 and 2) or more.

SO for example , we call function "getArrayValue()" and we can it like so:

getArrayValue('1,0,2')

this should return $array[1]['children'][0]['children'][2]['country']['city']

or in case of 2 values getArrayValue('1,0') should return $array[1]['children'][0]['country']['city']

so basically, the issue I am facing is needing help with dynamically building the query to get array value...

Or if there is a way to convert a string like $array[1]['children'][0]['country']['city'] and evaluate it to get this value from the array itself?

Another way to explain my problem:

$arrayIndex = "[1]['children'][0]";
$requiredValue = $array[1] . $arrayIndex . ['country']['city'];

//// so $requiredValue should output the same value as the below line would:
$array[1]['children'][0]['children'][2]['country']['city'];

Is there a way to achieve this?



source https://stackoverflow.com/questions/71279077/is-there-a-way-to-dynamically-access-multidimensional-php-array

No comments:

Post a Comment