0

I have a function with a non-defined size. Also, I have a function which received an unlimited number of arguments that represent the desired result's indexes. I want this function to return the value of the array, which those indexes related to.

In order to retrieve the data, I've built the following function:

public static function get() {
    $result = self::$config; // $config is the unknown sized array
    $args = func_get_args();
    foreach($args as $arg) {
        $result = $result[$arg];
    }
    return $result;
}

However, I didn't manage to build a "set" function.

How can I edit the source array ($config), based on the parameters inserted?

Example:

We have the following array:

$ron = array(
            'first' => array('inside' => 1),
            'second' => array('inside' => array('anotherInside' => 2),
            'lol' => 'lol'
        ),
        'third' => 3
    );

Now we are activating the function with the parameters as follows:

function retrieve('newValue', 'second', 'inside');

The function should edit the source to:

$ron = array(
            'first' => array('inside' => 1),
            'second' => array( **'inside' => 'newValue',**
            'lol' => 'lol'
        ),
        'third' => 3
    );

Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Ron Rofe
  • 738
  • 1
  • 9
  • 25

1 Answers1

0

Take there a fast forward solution for your problem.

private static $config = [
    'data' => [
        'data' => 'v1',
        'altceva' => 'v2'
    ],
    'sec' => [
        'data' => 'v3',
        'altceva' => 'v4'
    ],
    'thrs' => [
        'data' => 'v5',
        'altceva' => [
            'misc' => 'v6',
            'altceva' => 'v7',
            'super' => 'v8'
        ]
    ]
];

public static function get(){

    // get parameters
    $args = func_get_args();

    // new value
    $newValue = $args[0];

    // specified node
    $node = implode(".", array_values(array_slice($args, 1)));

    // encode config array
    $encoded = self::encodeArray(self::$config);

    // check if specified node exists
    if( ! isset($encoded[$node]) ){
        return false;
    }

    // change node content
    $encoded[$node] = $newValue;

    // do something with the result
    return self::decodeArray($encoded);


}

private static function encodeArray( $arr, $node = null ){

    $result = [];

    foreach( $arr as $thisKey => $val ){

        $currentKey = ($node != null ? $node.'.' : '') . $thisKey;

        if( is_array($val) ){   

            foreach( self::encodeArray($val, $currentKey) as $subKey => $subVal)
                $result[ $subKey ] = $subVal;

        } else {
            $result[ $currentKey ] = $val;
        }

    }


    // return result
    return $result;

}

private static function decodeArray( $arr ){

    $result = [];

    foreach( $arr as $key => $val ){

        $microKey = explode(".", $key);

        if( count($microKey) > 1 ){

            $innerKey = implode(".", array_slice($microKey, 1));

            if( isset($result[$microKey[0]] ) )
                $result[ $microKey[0] ] = array_merge_recursive( $result[ $microKey[0] ], self::decodeArray([$innerKey => $val]));
            else
                $result[ $microKey[0] ] = self::decodeArray([$innerKey => $val]);

        } else {
            $result[ $key ] = $val;
        }

    }

    return $result;

}

/*
* call your method
*/
print_r(YourClass::get('Kogaion', 'thrs', 'altceva', 'altceva'));
Mihai Zinculescu
  • 389
  • 2
  • 14