Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7550545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:14:24+00:00 2026-05-30T10:14:24+00:00

Is it possible to do something like this in PHP? $index1 = [0][1][2]; $index2

  • 0

Is it possible to do something like this in PHP?

$index1 = "[0][1][2]";
$index2 = "['cat']['cow']['dog']";

// I want this to be $myArray[0][1][2]
$myArray{$index1} = 'stuff';

// I want this to be $myArray['cat']['cow']['dog']
$myArray{$index2} = 'morestuff';

I’ve searched for a solution, but I don’t think I know the keywords involved in figuring this out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:14:25+00:00Added an answer on May 30, 2026 at 10:14 am

    Not directly. $myArray[$index] would evaluate to $myArray['[0][1][2]']. You would probably have to separate each dimension or write a little function to interpret the string:

    function strIndexArray($arr, $indices, $offset = 0) {
         $lb = strpos($indices, '[', $offset);
         if ($lb === -1) {
             return $arr[$indices];
         }
         else {
             $rb = strpos($indices,']', $lb);
             $index = substr($indices, $lb, $rb - $lb);
             return strIndexArray($arr[$index], substr($indices, $rb+1));
         }
    }
    

    You can probably find some regular expression to more easily extract the indices which would lead to something like:

    $indices = /*regex*/;
    $value = '';
    foreach($indices as $index) {
        $value = $array[$index];
    }
    

    To set a value in the array the following function could be used:

    function setValue(&$arr, $indices, $value) {
        $lb = strpos($indices, '[');
        if ($lb === -1) {
            $arr = $value;
        }
        else {
            $rb = strpos($indices, ']', $lb);
            $index = substr($indices, $lb, $rb);
            setValue($arr[$index], substr($indices, $lb, $rb+1), $value);
        }
    }
    

    Note: I made above code in the answer editor so it may contain a typo or two ; )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to do something like this in PHP 5.2.1? abstract class Test
Is something like this possible: <form method=post action=myphp.php name=myname target=another_html/iframe_name>??? I want the php
Is it possible to do something like this in a php if statement: if($a
Is it possible to do something like this? var pattern = /some regex segment/
Is it possible to do something like this: <% Dim foo as string =
is it possible to create something like this i ASP.NET MVC beta 1 i
Is it possible to do something like this? public function something() { $thisMethodName =
is it possible to do something like this in JSTL: <div class=firstclass<c:if test='true'> someclass</c:if>>
Is it possible to accomplish something like this using linqtosql? select * from table
Is it possible to use something like this wrapper with fluent configuration? http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/ If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.