0

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")
6
  • 5
    @GrumpyCrouton Kind of nonsense, either you do use sprintf() or you don't. I would suggest you do, so sprintf("A cool new store called %s has been added please login to administrators area to approve the request", $fields['name']['message']). Commented Jan 7, 2019 at 17:06
  • Whoever voted to close this question as "not clear": I think it's quite clear indeed. Commented Jan 7, 2019 at 17:31
  • @arkascha Thanks, I'm not very familiar with sprintf Commented Jan 7, 2019 at 17:38
  • @arkascha thank you for the reply, but im still getting "please enter your store name" in place fo where the store name should be . if i removed the [message] part, i get "A cool new store called Array has been added please login to administrators area to approve the request" Commented Jan 7, 2019 at 18:40
  • @bsgtesting Where is the name of the store stored? What variable? Commented Jan 7, 2019 at 18:41

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the reply, but im still getting "please enter your store name" in place fo where the store name should be . if i removed the [message] part, i get "A cool new store called Array has been added please login to administrators area to approve the request"
Can you tell me where you have your store name stored & how you get to it?
thank you for the reply. i have a file called en_US.php. inside iit is all these LANG lines. the LANG lines in my code above is the$lang['ADMIN_STORE_NAME_VALIDATE'] inside the en_US.php it is assigned the following sentence $lang['ADMIN_STORE_NAME_VALIDATE'] = "Please enter a Store Name";
So the actual name of the store is not stored in $lang['ADMIN_STORE_NAME_VALIDATE']?
1

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.

3 Comments

true^^ though I'd prefer to concat for readability purposes, sometimes you can miss seeing vars inside a string
Awesome ^.^ removed comment :)
thank you for the reply, but im still getting "please enter your store name" in place fo where the store name should be . if i removed the [message] part, i get "A cool new store called Array has been added please login to administrators area to approve the request"

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.