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

  • Home
  • SEARCH
  • 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 6669451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:09:05+00:00 2026-05-26T03:09:05+00:00

can’t figure out how to insert a new string in a specific emplacement in

  • 0

can’t figure out how to insert a new string in a specific emplacement in an array regarding a given uri.

to be more specific,

I’m trying to create a function like the following one :

function insertNewFolder($name, $path){

  $struct = json_decode( file_get_contents('structure.json'), true );

  //at this place I want to include $name in the array according the given $path
}

let’s assume $struct is the following array :

$struct = array( 'folder' => array('subfolder1', 'subfolder2') );

NOTE: the scope can be more than one subfolder.
example : array( 'folder' => array( 'subfolder' => array( 'subsubfolder' )));

If I call the method like this insertNewFolder('subfolder3', 'folder'); the resulting array should be :

$struct = array( 'folder' => array('subfolder1', 'subfolder2', 'subfolder3') );

this is my try :

function insertNewFolder($name, $path){

  ...

  // insert the new folder according the path
  $segments = explode('/', $path);

  $arr_path = '';
  foreach( $segments as $s ){
    $arr_path .= "['$s']";
  }
  eval("/$menu$arr_path[] = '$name';");

  ...
}

This would work if the eval method weren’t throwing an exception :

Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 16

I beg your help, is there a better way to reach this end or how to round this exception ?

Regards

  • 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-26T03:09:06+00:00Added an answer on May 26, 2026 at 3:09 am

    You can do something similar to this (demo):

    function insertValueByPath($array, $path, $value) {
        $current = &$array;
        foreach (explode('/', $path) as $part) {
            $current = &$current[$part];
        }
        $current = $value;
    
        return $array;
    }
    
    $struct = array(
        'folder' => array(
            'subfolder1' => array(),
            'subfolder2' => array(),
        ),
    );
    
    $struct = insertValueByPath($struct, 'folder/subfolder1/subsubfolder', array());
    $struct = insertValueByPath($struct, 'folder/subfolder2/subsubfolder/subsubsub/subsubsubsub', array('hallo'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Can you cast a List<int> to List<string> somehow? I know I could loop through
Does anyone know how can I replace this 2 symbol below from the string
Can someone help me? I am trying to do something like the following: #include
Can the Application Name passed to SQL Server (via the ADO Connection string) be
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
can you recommend some good ASP.NET tutorials or a good book? Should I jump

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.