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 6816331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:54:46+00:00 2026-05-26T20:54:46+00:00

I have a class that keeps data stores/access data by using words.separated.by.dots keys and

  • 0

I have a class that keeps data stores/access data by using words.separated.by.dots keys and it behaves like the following:

    $object = new MyArray()
    $object->setParam('user.name','marcelo');
    $object->setParam('user.email','some@email.com');

    $object->getParams();

    /*
    array(
        'user' => array(
            'name' => 'marcelo',
            'email' => 'some@email.com'
        )
    );
    */

It is working, but the method unsetParam() was horribly implemented. That happened because i didn’t know how to achieve that without eval() function. Although it is working, I found that it was a really challenging algorithm and that you might find fun trying to achieve that without eval().

class MyArray {
   /**
     * @param string $key
     * @return Mura_Session_Abstract 
     */
    public function unsetParam($key)
    {
        $params = $this->getParams();
        $tmp = $params;
        $keys = explode('.', $key);

        foreach ($keys as $key) {
            if (!isset($tmp[$key])) {
                return $this;
            }
            $tmp = $tmp[$key];
        }

        // bad code!
        $eval = "unset(\$params['" . implode("']['", $keys) . "']);";
        eval($eval);

        $this->setParams($params);
        return $this;
    }
}

The test method:

public function testCanUnsetNestedParam()
{
    $params = array(
        '1' => array(
            '1' => array(
                '1' => array(
                    '1' => 'one',
                    '2' => 'two',
                    '3' => 'three',
                ),
                '2' => array(
                    '1' => 'one',
                    '2' => 'two',
                    '3' => 'three',
                ),
            )
        ),
        '2' => 'something'
    );

    $session = $this->newSession();
    $session->setParams($params);

    unset($params['1']['1']['1']);
    $session->unsetParam('1.1.1');

    $this->assertEquals($params, $session->getParams());
    $this->assertEquals($params['1']['1']['2'], $session->getParam('1.1.2'));
}
  • 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-26T20:54:47+00:00Added an answer on May 26, 2026 at 8:54 pm

    Is this it?

    <?php
    $params = array(
        '1' => array(
            '1' => array(
            '1' => array(
                '1' => 'one',
                '2' => 'two',
                '3' => 'three',
            ),
            '2' => array(
                '1' => 'one',
                '2' => 'two',
                '3' => 'three',
            ),
            )
        ),
        '2' => 'something'
        );
    
    function unsetParam( &$array, $paramString ) {
    $cur =& $array;
    $splitted = explode( ".", $paramString );
    $len = count( $splitted ) - 1;
    
        for( $i = 0; $i < $len; ++$i ) {
    
            if( isset( $cur[ $splitted[ $i ] ] ) ) {
            $cur =& $cur[ $splitted[ $i ] ];
            }
            else {
            return false;
            }
    
    
        }
    
    unset( $cur[ $splitted[$i] ] );
    
    
    }
    
    unsetParam( $params, "1.1.1");
    
    print_r( $params );
    
    /*
    Array
    (
        [1] => Array
        (
            [1] => Array
            (
                [2] => Array
                (
                    [1] => one
                    [2] => two
                    [3] => three
                )
    
            )
    
        )
    
        [2] => something
    )
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setup that looks like this. class Checker { // member data
I have a multi-R/W lock class that keeps the read, write and pending read
I have a class to parse a matrix that keeps the result in an
I have a class that I wish to expose as a remote service using
i have a data access layer that abstracts the rest of the application away
I have a data structure that accepts an abstract class in the c'tor. I'd
I have a class that I've written a TypeConverter for. I want to keep
I have a view controller class that has to implement several protocols. Too keep
We have a class library where we keep a lot of the stuff that
I have class that represents users. Users are divided into two groups with different

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.