10

Is there a way to index an array with an index sequence in a variable in PHP?

 2 years ago
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.
neoserver,ios ssh client

Is there a way to index an array with an index sequence in a variable in PHP?

advertisements

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.

Try this code snippet here

<?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?

Basically, I'm trying to do this: char x[] = "hello"; char* y = new char[sizeof(x)](x); // won't work. demo Is there a way to do this cleanly? No comments on DO NOT USE raw arrays, or raw pointers please.Just write a function. template<typena

Is there a way to use inherited methods with the same name of some classes?

Is there a way to use inherited methods with a same name from few classes, but without casting? public class A { someMethod(A a) {} } public class B { someMethod(B b) {} } public class Client : A, B { public Client() { someMethod(new A()); someMethod

Is there any way to remove photo labels with the Facebook API?

There are some old posts related to this topic but the API has since changed, so I wanted to ask again. Is there any way to remove photo tags with the Facebook API? I saw this documentation about deleting photo tags with the graph API. https://develo

Is there a way to integrate CommonJ WorkManager with Glassfish

Is there a way to integrate CommonJ WorkManager with GlassfishI've never done this myself but according to Sahoo (in this thread), the way to go would be to write a JCA connector and to use the javax.resource.spi.work.WorkManager API (which is part o

Is there a way to send typed messages with my own classes to a remote Akka actor?

Is there a way to send messages typed with my own classes to a remote actor? For instance I would like to be able to received in my remote actor a message like this: case myClass: MyClass => doSomething() But I get an error local class incompatible b

Is there a way to decorate multiple properties with DataMember in a data contract in WCF?

I've got a basic WCF service. I've split out my data contracts and one of them is a class with a large amount of public properties. If I want to make these properties available to the client I'm assuming all of these will need to have [DataMember]. S

Is there a way to convert an array to Ruby as a float by removing the brackets?

I have a little problem that seems rather easy to solve but I can't seem to find a solution. I'm using the Geocoder gem for Rails and from this method of: Geocoder.coordinates(params[:search]) I'm getting an array that contains floats like this: [43.

Is there a way to access an array of col markers for a javascript table?

I have a table that looks like: <table id="myTable"> <col id="name" /> <col id="birthYear" /> <col id="phone" /> <td> <tr>Joe</tr> <tr>1972</tr> <tr>202-55

Is there a way to merge two arrays into a single array in MongoDB?

I'm kinda new in MongoDB and I'm having trouble to achieve a certain task. I'll try to explain it. Here is my collection: 'workers': [ { _id: ObjectId(...), name: 'John', skills: [ { id: ObjectId('111'), value: 2 }, { id: ObjectId('222'), value: 3 },

Is there a way to select html elements with a certain class with ruby?

If I use require 'net/http' source = Net::HTTP.get('stackoverflow.com', '/index.html') to extract the source code from a url, is there a way, in ruby, to find all link elements with a certain class, and then to extract the href attribute of those url

Is there a way to use Delphi Inputbox with multiple values?

I want to get multiple different types values (time, pressure, name, ...) from user. and I want to customize my inputbox. But it take only one value. Are there any way to solve this situation, or any component to use?François is right. You've reached

Is there a way to disable all warnings with a pragma?

I've started a new project and have decided to make sure it builds cleanly with the /Wall option enabled. The only problem is not all 3rd party libraries (like boost) compile without warnings, so I've resorted to doing this in a shared header: #pragm

the fastest way to compare the array with the incoming buffer * byte *

Is there any fastest way to compare 24 element byte array with incoming byte* buffer? byte compare_array[24]= {0x00,0x00,0x00,0x01,0x26,0x05, 0xF8,0x00,0x11,0x22,0x33,0x44, 0x55,0x66,0x77,0x88,0x99,0xAA, 0xBB,0xCC,0xDD,0xEE,0xFF,0xFF}; below code doe

Is there a way to configure a redirect with a workaround?

Is there any way to set up a redirect with a bypass url such as: http://example.com/oldpage?bypass=1 That would bypass the redirect. But http://example.com/oldpage Would redirect to http://example.com/newpage Thank you for the answers, I've managed t

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK