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.

Link to comment
Share on other sites

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;





}

}

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.