bsg testing
bsg testing

Reputation: 9

how can I append an array value to a string?

I'd like to append an array value to the middle of a string.

I tried making the name as new variable and append the variable. I looked up various ways to grab just one value out of array, but nothing satisfied my needs.

I have this array:

$fields = array(
    'name' => array(
        'rule' => '/.+/',
        'message' => $lang['ADMIN_STORE_NAME_VALIDATE'],
        'value' => '',
        'required' => TRUE
    ),

I want to append the store name to say, the end of this string

sprintf("A cool new store has been added  please login to administrators area to approve the request")

Upvotes: 0

Views: 109

Answers (2)

Aleks G
Aleks G

Reputation: 57316

In PHP you can include variable values in the string using directly for simple variable or using ${ } notation for arrays/objects/etc. So something like this would work:

echo "A cool new store called {$fields['name']['message']} has been added please login to administrators area to approve the request";

As noted by @treyBake, for the variable expansion to work, you must use double quotes; variables are not expanded in single-quoted strings.

Upvotes: 1

lucid
lucid

Reputation: 422

The . is a concatenation operator. Therefore, doing something like the following:

$sentence = "A cool new store called " . $fields['name']['message'] . " has been added please login to administrators area to approve the request";

Is going to give you the result you want.

For more info: http://www.php.net/manual/en/language.operators.string.php

Upvotes: 1

Related Questions