fawkstrot Posted March 30, 2006 Share Posted March 30, 2006 Ultimately I ended up giving up on this and going with what I found to be the only way to accomplish what I needed to be done. I'm hoping you all here might have some insight and perhaps there's an easier way of doing what I am. This is what I am currently having to use:[code]$array_test = array ( 'testelement' => "Blah #holder_name#", 'testelement2' => "Hello world", ); function test_function(){ global $array_test; $newvalue="stupid code"; $array_test['testelement']=str_replace('#holder_name#',$newvalue,$array_test['testelement']); echo $array_test['testelement']; echo "<br/><br/>"; echo $array_test['testelement2']; }test_function(); [/code]This outputs:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Blah stupid codeHello world[/quote]Consider $array_test basically a language array for a specific function. In that function I would like to replace certain values (for example the user name) with a value defined in the function. I tried doing this through various globals, etc., and just couldn't figure it out. Ultimately what I ended up having to do is put in the str_replace portion to put in the dynamic data I wanted inside of the array element.Ultimately if something like this could work (I know the code I'm going to paste won't, but hopefully you'll get the idea) I'd be very pleased...[code]$array_test = array ( 'testelement' => "Blah $holder_name", 'testelement2' => "Hello world", ); function test_function(){ global $array_test; $holder_name="stupid code"; echo $array_test['testelement']; echo "<br/><br/>"; echo $array_test['testelement2']; }test_function(); [/code]If I could get that to output the same, or something like that, as the example above, I'd be greatful. It seems to me there has to be an easier way to do what I want to do. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/6136-variable-within-an-array-element/ Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 Something like this:[code]<?phpfunction lang_output( $langkey , $param ){ var $lang = array( 'element' => 'This is a {param} message' ); var $msg = ( isset( $lang[ $langkey ] ) ) ? str_replace( "{param}" , $param , $lang[ $langkey ] ) : "An unknown error occured"; return $msg;}?>[/code]Example of usage:[code]<?phpecho lang_output( 'element' , 'test' );?>[/code]Should output: This is a test messageUntested, but it gives you an idea of the best way to do this. To answer your question, you cannot use undefined variables in an array, they must be defined first or you will get errors (assuming E_NOTICE is enabled in error_reporting) Quote Link to comment https://forums.phpfreaks.com/topic/6136-variable-within-an-array-element/#findComment-22132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.