Is there a way to index an array with an index sequence in a variable in PHP?
source link: https://www.codesd.com/item/is-there-a-way-to-index-an-array-with-an-index-sequence-in-a-variable-in-php.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Is there a way to index an array with an index sequence in a variable in PHP?
Having: an array $a, a variable $indexes = "[\"level1\"][\"level2\"][\"level3\"]"
;
Is there any way to access $a["level1"]["level2"]["level3"]
?
The situation is that the number of indexes that this function will handle can change. So this is the reason the indexes comes in a variable.
Hope this simple solution will help you out.
<?php
ini_set('display_errors', 1);
//Here we are retrieving levels
$indexes = '["level1"]["level2"]["level3"]';
preg_match_all('/(?<=")[\w]+(?=")/', $indexes,$matches);
$levels=$matches[0];
//this is the sample array
$array=$tempArray=array(
"level1"=>array(
"level2"=>array(
"level3"=>"someValue"
)
)
);
//here we are iterating over levels to get desired output.
foreach($levels as $level)
{
$tempArray=$tempArray[$level];
}
print_r($tempArray);
Related Articles
Is there a way to initialize an array with a buffer at the time of declaration?
Is there a way to use inherited methods with the same name of some classes?
Is there any way to remove photo labels with the Facebook API?
Is there a way to integrate CommonJ WorkManager with Glassfish
Is there a way to send typed messages with my own classes to a remote Akka actor?
Is there a way to decorate multiple properties with DataMember in a data contract in WCF?
Is there a way to convert an array to Ruby as a float by removing the brackets?
Is there a way to access an array of col markers for a javascript table?
Is there a way to merge two arrays into a single array in MongoDB?
Is there a way to select html elements with a certain class with ruby?
Is there a way to use Delphi Inputbox with multiple values?
Is there a way to disable all warnings with a pragma?
the fastest way to compare the array with the incoming buffer * byte *
Is there a way to configure a redirect with a workaround?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK