【问题标题】:How to put multidimensional arrays double quoted strings?如何将多维数组双引号字符串?
【发布时间】:2016-10-29 11:57:15
【问题描述】:

我知道我可以在双引号中使用数组值。像这样:

<?php echo "my name is: $arr[name]"; ?>

但是当我使用多维数组时,我看不到我的结果:

<?php echo "he is $twoDimArr[family][1]"; ?>

这里的输出是:he is Array[1]

什么原因?

而且我知道我可以像这样使用我的代码:

<?php echo "he is ".$twoDimArr[family][1]; ?>

但我不想要这个。

【问题讨论】:

标签: php arrays string


【解决方案1】:

你应该用花括号括起来更复杂的结构:

echo "he is {$twoDimArr['family'][1]}";

【讨论】:

    【解决方案2】:

    你应该这样做,使用花括号{ & }

    echo "he is {$twoDimArr['family'][1]}";
    


    请参阅String parsing documentationecho() function 了解更多详细信息(尤其是示例#1):
    // You can also use arrays  
    $baz = array("value" => "foo");
    
    echo "this is {$baz['value']} !"; // this is foo !
    
    // Using single quotes will print the variable name, not the value  
    echo 'foo is $foo'; // foo is $foo
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-27
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多