Jump to content

Recommended Posts

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

[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.
Link to comment
https://forums.phpfreaks.com/topic/6136-variable-within-an-array-element/
Share on other sites

Guest footballkid4
Something like this:

[code]<?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;
}
?>[/code]

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

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