Jump to content


Photo

variable within an array element


  • Please log in to reply
1 reply to this topic

#1 fawkstrot

fawkstrot
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 30 March 2006 - 12:08 AM

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:



$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();  

This outputs:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Blah stupid code

Hello 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...

$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();

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.

#2 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 12:13 AM

Something like this:

<?php
function 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;
}
?>

Example of usage:
<?php
echo lang_output( 'element' , 'test' );
?>

Should output: This is a test message

Untested, 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)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users