0

How to use a stored string which contains a variable?

Example:

I have to gather error notifications strings from the database. These notifications are divided into two types. Regular (common, frequent) strings and targeted (specifically intended) strings.

Let's assume that I have already fetched regular strings and the array was created.

print_r ( $reg_errs );
Array ( [unexp_err] => Array (
  [eng] => An unexpected error has become.
) )

Then I have fetched the array of targeted errors

print_r ( $targ_errs );
Array ( [incorr_cfg_langs] => Array (
  [eng] => $reg_errs['unexp_err']['eng'] . ' Incorrect configuration settings of language versions.'
) )

.

echo $targ_errs['incorr_cfg_langs']['eng'];

The result will be:

$reg_errs['unexp_err']['eng'] . ' Incorrect configuration settings of language versions.'

The requested result is:

An unexpected error has become. Incorrect configuration settings of language versions.

Is there a way to reach my goal? Thank you for any advice ☺

EDIT:

if ( $stmt = $mysqli -> prepare ( "..." ) ) {
    $stmt -> execute ();
    $res = $stmt -> get_result ();
    if ( mysqli_num_rows ( $res ) != NULL ) {
        while ( $row = $res -> fetch_assoc () ) {
            $error = str_replace ( ' ', '', $row['error_assoc_id'] );
            $error_chars_no = mb_strlen ( $error );
            $lang_iso = str_replace ( ' ', '', $row['lang_iso'] );
            $lang_iso_chars_no = mb_strlen ( $lang_iso );
            $value = trim ( $row['value'] );
            $value_chars_no = mb_strlen ( $value );
            if ( ( $error_chars_no >= 3 ) && ( $lang_iso_chars_no == 3 ) && ( $value_chars_no >=3 ) ) {
                $targ_errs[$error][$lang_iso] = $value;
            }
        }
     }
}

}

11
  • So you are saying that $reg_errs['unexp_err']['eng'] . ' Incorrect configuration settings of language versions.' is becoming as single string ("$reg_errs['unexp_err']['eng'] . ' Incorrect configuration settings of language versions.'") instead of variable and string concatenation? Are you hiding some code from us? Commented Dec 1, 2014 at 13:27
  • "An expected error has become" is just the string inside $reg_errs['unexp_err']['eng']. Commented Dec 1, 2014 at 13:30
  • I have edited echo, there was ['unexp_err'] key and now it is correct. Yes, the result is like you see it in my question and I am not hiding any relevant code. Commented Dec 1, 2014 at 13:30
  • Could you please post code, instead of print_r output? Commented Dec 1, 2014 at 13:30
  • @AwesomeGuy What code exactly? Commented Dec 1, 2014 at 13:31

1 Answer 1

2

What about this,

$sp_err = array ( 'unexp_err' => array (
  'eng' => 'An unexpected error has become.'
) );



$reg_err = array ( 'incorr_cfg_langs' => array (
  'eng' => $sp_err['unexp_err']['eng'] . ' Incorrect configuration settings of language versions.'
) );

echo $reg_err['incorr_cfg_langs']['eng'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, but it looks like the same what I have now.
This should really work, great PHP, and should work as intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.