[英]multi dimensional arrays using PHP
我是使用php的新手,并且想知道如何输出muilti三维数组。 这是我的测试代码:
<html>
<body>
<p>
<?php
$test2 = array(
array("!","#","#"),
array("@","!","#"),
array("@","@","!",)
);
echo $test2[0][0]."-".$test2[0][1]."-".$test2[0][2]."<br>";
echo $test2[1][0]."-".$test2[1][1]."-".$test2[1][2]."<br>";
echo $test2[2][0]."-".$test2[2][1]."-".$test2[2][2]."<br>";
echo "<br>";
for($f=0; $f < count($test2); $f++)
{
for($g=0; $g < count($test2); $g++)
{
echo "<br>$test2[$f][$g] - ";
}
}
?>
</p>
</body>
</html>
每当我对此进行测试时,都会收到错误消息“注意:C:\\ wamp \\ www \\ Test.php中的数组到字符串的转换”
您不应回显手动选择的数组:
<?php
$test2 = array(
array("!","#","#"),
array("@","!","#"),
array("@","@","!",)
);
echo $test2[0][0]."-".$test2[0][1]."-".$test2[0][2]."<br>";
echo $test2[1][0]."-".$test2[1][1]."-".$test2[1][2]."<br>";
echo $test2[2][0]."-".$test2[2][1]."-".$test2[2][2]."<br>";
echo "<br>";
for($f = 0; $f < count($test2); $f++) {
for($g = 0; $g < count($test2[$f]); $g++) {
$value = $test2[$f][$g];
echo "{$value} - ";
}
echo '<br />';
}
或者您也可以尝试使用foreach
<?php
$test2 = array(
array("!","#","#"),
array("@","!","#"),
array("@","@","!",)
);
echo $test2[0][0]."-".$test2[0][1]."-".$test2[0][2]."<br>";
echo $test2[1][0]."-".$test2[1][1]."-".$test2[1][2]."<br>";
echo $test2[2][0]."-".$test2[2][1]."-".$test2[2][2]."<br>";
echo "<br>";
//--> First foreach for extracting sub arrays
foreach($test2 as $test1) {
//--> Second foreach for extracting values of sub arrays
foreach($test1 as $test) {
echo "{$test} - ";
}
echo '<br />';
}
最后,这是一个懒惰的方法:您也可以尝试制作一个仅输出任何值的函数:
function io($a = 0) {
echo '<pre>';
print_r($a);
echo '</pre>';
}
您面临的问题在两个循环中:
for($f=0; $f < count($test2); $f++)
{
for($g=0; $g < count($test2[$f]); $g++)
{
echo "<br>$test2[$f][$g] - ";
}
}
将变量嵌入双引号字符串中没问题,但是如果它们很复杂,则必须将它们括在大括号中。 原因很简单,如果在同一条语句中使用单引号,请看PHP在回显后如何解释字符串。
echo '<br>' , $test2 , '[' , $f , '][' , $g , '] - ';
for($f=0; $f < count($test2); $f++)
{
for($g=0; $g < count($test2[$f]); $g++)
{
echo "<br>{$test2[$f][$g]} - ";
}
}
PS:当然没有正确的方法。 我用这个短语来指代您想用原始代码实现的目标 。
您不能像这样将数组放入双引号字符串中。 它将在$test2
处中断。 (也就是说,它认为您实际上要说的是(将数组转换为字符串)[0] [2]。)
有效的解决方案:
A.将变量放在花括号中,这将导致PHP将整个位解析为变量。
<?php
$test2 = array(
array("!","#","#"),
array("@","!","#"),
array("@","@","!",)
);
echo $test2[0][0]."-".$test2[0][1]."-".$test2[0][2]."<br>";
echo $test2[1][0]."-".$test2[1][1]."-".$test2[1][2]."<br>";
echo $test2[2][0]."-".$test2[2][1]."-".$test2[2][2]."<br>";
echo "<br>";
for($f=0; $f < count($test2); $f++)
{
for($g=0; $g < count($test2); $g++)
{
echo "<br>{$test2[$f][$g]} - ";
}
}
B.使用合并。
<?php
$test2 = array(
array("!","#","#"),
array("@","!","#"),
array("@","@","!",)
);
echo $test2[0][0]."-".$test2[0][1]."-".$test2[0][2]."<br>";
echo $test2[1][0]."-".$test2[1][1]."-".$test2[1][2]."<br>";
echo $test2[2][0]."-".$test2[2][1]."-".$test2[2][2]."<br>";
echo "<br>";
for($f=0; $f < count($test2); $f++)
{
for($g=0; $g < count($test2); $g++)
{
echo "<br>" . $test2[$f][$g] . " - ";
}
}
C.使用foreach()循环代替for()循环。
<?php
$test2 = array(
array("!","#","#"),
array("@","!","#"),
array("@","@","!",)
);
echo $test2[0][0]."-".$test2[0][1]."-".$test2[0][2]."<br>";
echo $test2[1][0]."-".$test2[1][1]."-".$test2[1][2]."<br>";
echo $test2[2][0]."-".$test2[2][1]."-".$test2[2][2]."<br>";
echo "<br>";
foreach($test2 as $inner_array)
{
foreach($inner_array as $item)
{
echo "<br>$item - ";
}
}
我通常自己使用串联,因此在浏览代码时很容易看到变量,但是如果不这样做,则应始终将变量放在花括号中以避免混淆。 如果您想对某些内容进行复数操作,这也很重要... {$item}s
将打印出$item
并附加一个s。
不过,foreach()循环在这里可能更有意义(我还将括号放在变量中,该变量未在代码中显示。)
您需要在循环中将数组与其余字符串分开。 有两种方法可以做到这一点:
echo "<br>{$test2[$f][$g]} - ";
要么
echo "<br>".$test2[$f][$g]." - ";
现在,它正在尝试先打印$ test2,然后打印$ f,然后打印$ g。 $ test2正在给出错误。
您可以使用print_r($ test2)在循环中获取所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.