PHP:多维数组元素的串联 [英] PHP: concatenation of multidimensional array elements
本文介绍了PHP:多维数组元素的串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用一些字符串连接多维数组的一个元素。
I want to concatenate one element of a multidimensional array with some strings.
<?
$string1 = 'dog';
$string2 = array
(
'farm' => array('big'=>'cow', 'small'=>'duck'),
'jungle' => array('big'=>'bear', 'small'=>'fox')
);
$string3 = 'cat';
$type = 'farm';
$size = 'big';
$string = "$string1 $string2[$type][$size] $string3";
echo($string);
?>
通过对$ string使用此语法,我得到:
By using this syntax for $string, I get:
狗数组[大]猫
我不想使用其他语法
$string = $string1 . ' ' . $string2[$type][$size] . ' ' . $string3;
有效。
出什么问题了 $ string1 $ string2 [$ type] [$ size] $ string3?
What's wrong with "$string1 $string2[$type][$size] $string3"?
推荐答案
使用复杂语法:
$string = "$string1 {$string2[$type][$size]} $string3";
PHP的变量解析非常简单。它将识别一级阵列访问,但不能识别更多级。通过将表达式包含在 {}
中,您可以明确说明字符串的哪一部分是变量。
PHP's variable parsing is quite simple. It will recognize one level array access, but not more level. By enclosing the expression in {}
you explicitly state which part of the string is a variable.
请参见 PHP-变量解析。
这篇关于PHP:多维数组元素的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文