简体   繁体   English

如何获得多维数组的回声?

[英]How to get an echo of a multidimensional array?

i got this Code:我得到了这个代码:

$Wanteds[] = array(
                'WantedLevel' => 1,
                'WantedName' => "Nichtbefolgen",
                'WantedPreis' => 300
            );
$Wanteds[] = array(
                'WantedLevel' => 1,
                'WantedName' => "Beihilfe",
                'WantedPreis' => 200
            );
$Wanteds[] = array(
                'WantedLevel' => 2,
                'WantedName' => "Dealen",
                'WantedPreis' => 500
            );

Now, i tryed to get it into a variable:现在,我试着把它变成一个变量:

echo "TESTING: $Wanteds[0][WantedName] <br />";

All i got it: TESTING: [WantedName] instead of: TESTING: Nichtbefolgen我只知道:测试:[WantedName] 而不是:测试:Nichtbefolgen

Please keep in mind: I'm new to arrays and programming.请记住:我是数组和编程的新手。 :) :)

Depends on what you want.取决于你想要什么。

Echo 1 value回显 1 值

echo "TESTING: " . $Wanteds[0]['WantedName'] . "<br />"; // possibility 1
echo "TESTING: {$Wanteds[0]['WantedName']} <br />"; // possibility 2

Print entire array打印整个数组

print_r($Wanteds); // Usefull for debugging and value checking

你必须用大括号对字符串中的变量进行warb

echo "TESTING: {$Wanteds[0]['WantedName']} <br />";

No need to print variables in double quotes.无需在双引号中打印变量。

You can print it and concatenate it to string like this:您可以打印它并将其连接成这样的字符串:

echo "TESTING: " .$Wanteds[0]['WantedName'] ."<br />";

Putting variables cause extra load on server as he needs to parse everything inside the string.放置变量会导致服务器额外负载,因为他需要解析字符串中的所有内容。

Above phenomena is called variable interpolation.上述现象称为变量插值。

You can use strings without double quotes and that is faster than embedding them in double quotes.您可以使用没有双引号的字符串,这比将它们嵌入双引号中要快。

See it live here在这里观看直播

尝试这个

echo "TESTING: ".$Wanteds[0]['WantedName']." <br />";   

To return the name that you want you can do the following :要返回您想要的名称,您可以执行以下操作:

echo "TESTING: " .$Wanteds[0]['WantedName'] ."<br />";

You could also display the whole array with :您还可以使用以下命令显示整个数组

print_r($Wanteds);

Or even display each sub-array separatly by doing :甚至通过执行以下操作单独显示每个子数组

foreach($Wanteds as $wanted){
 print_r($wanted);
}

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

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