maexus Posted June 11, 2008 Author Share Posted June 11, 2008 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 https://forums.phpfreaks.com/topic/109638-solved-how-to-you-explode-a-string-and-use-each-part-in-assoc-array/page/2/#findComment-562820 Share on other sites More sharing options...
grimmier Posted June 11, 2008 Share Posted June 11, 2008 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 https://forums.phpfreaks.com/topic/109638-solved-how-to-you-explode-a-string-and-use-each-part-in-assoc-array/page/2/#findComment-562872 Share on other sites More sharing options...
maexus Posted June 11, 2008 Author Share Posted June 11, 2008 Testing this out but I think that does it. It works! Thanks, seriously and thanks to everyone who posted. Link to comment https://forums.phpfreaks.com/topic/109638-solved-how-to-you-explode-a-string-and-use-each-part-in-assoc-array/page/2/#findComment-562879 Share on other sites More sharing options...
grimmier Posted June 11, 2008 Share Posted June 11, 2008 glad to hear it. keep me posted, still curious if it will work when stuck into your application. Link to comment https://forums.phpfreaks.com/topic/109638-solved-how-to-you-explode-a-string-and-use-each-part-in-assoc-array/page/2/#findComment-562884 Share on other sites More sharing options...
maexus Posted June 11, 2008 Author Share Posted June 11, 2008 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 https://forums.phpfreaks.com/topic/109638-solved-how-to-you-explode-a-string-and-use-each-part-in-assoc-array/page/2/#findComment-562892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.