简体   繁体   English

如何将数组值附加到字符串?

[英]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")

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 有关更多信息: http : //www.php.net/manual/zh/language.operators.string.php

In PHP you can include variable values in the string using directly for simple variable or using ${ } notation for arrays/objects/etc. 在PHP中,您可以在字符串中包含变量值,直接将其用于简单变量,将${ }表示法用于数组/对象/等。 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; 如@treyBake所述,要使变量扩展起作用, 必须使用双引号; 否则必须使用双引号。 variables are not expanded in single-quoted strings. 变量不会在单引号字符串中扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM