Jump to content

[SOLVED] How to you explode a string and use each part in assoc array?


maexus

Recommended Posts

Ok, loaded this in and ran it. It looked promising as I wasn't getting just the usually error. I first tried get("db")

 

Notice: Undefined variable: this->settings in settings.class.php on line 77
Array ( [db] => Array ( ) ) 

 

I then tried get("db__host") and get:

 

Array ( [db] => Array ( ) [host] => Array ( ) )

 

db is a key in the $settings array, a base key. host is a key of db. So the get() function should return the db array, db should appear in the array. This is what the array should look like if the function is working right when using get("db"):

 

Array ( [host] => localhost [username] => root [password] => root [database] => test [prefix] => ) 

 

Going back and reviewing the first page, effigy suggested using eval. Any other ideas in that direction as I'm completely dry.

something else to try. taking into consideration eval

 


public function get($variable){
		$varArray = explode('__', $variable);
		$temp_var = "this->settings";
		                    
		foreach($varArray as $key=>$value) { 

		$temp_var .= "['".$value."']";      
                                          
		}

                         $z = eval("return \$$temp_var;");
		  return $z;

// Or to keep it similar to your first ones. return string

// return (isset($z)) ? $z : $z;





}

}

 

 

Looks good, seems to work fine when I replaced the old get() method. There is an oddity. If there is one too many indexes, it only outputs the first character of the previous index.

 

Like if I select get("db__host__test") and db__host is "LOCALHOST" but the index test doesn't exist, it will return "L". However, if I add another non existent index get("db__host__test__another") it outputs the following error:

 

Fatal error: Cannot use string offset as an array

 

Like I said, an oddity, just a warning for anyone who may use the code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.