falcon
falcon

Reputation: 357

Using stored variables with PHP

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;
            }
        }
     }
}

}

Upvotes: 0

Views: 61

Answers (1)

Dimag Kharab
Dimag Kharab

Reputation: 4519

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'];

Upvotes: 2

Related Questions