Php 联想;多维的;大堆
大家好,我有一个简短的问题,希望你们能回答。我正在使用多维数组,我应该声明一个小、中、大盒子的宽度、长度和深度,然后添加显示每个盒子体积的语句。我的问题来自于这个语句(我是PHP新手)。在WAMP上运行脚本时,返回一个空白页,因此我猜测我做错了什么。这是代码,谢谢你的帮助Php 联想;多维的;大堆,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,大家好,我有一个简短的问题,希望你们能回答。我正在使用多维数组,我应该声明一个小、中、大盒子的宽度、长度和深度,然后添加显示每个盒子体积的语句。我的问题来自于这个语句(我是PHP新手)。在WAMP上运行脚本时,返回一个空白页,因此我猜测我做错了什么。这是代码,谢谢你的帮助 <?php $SmallBox =array("length" =>12, "width" =>10, "depth" =>2.5); $MediumBox =array("length" =>3
<?php
$SmallBox =array("length" =>12, "width" =>10, "depth" =>2.5);
$MediumBox =array("length" =>30, "width" =>20, "depth" =>4);
$LargeBox =array("length" =>60, "width" =>40, "depth" =>11.5);
echo $SmallBox ["length"]["width"] ["depth"];
echo $MediumBox ["length"] ["width"] ["depth"];
?>
虽然您已经正确创建了数组,但是您的echo语句不可能以这种方式创建 尝试: 依此类推。使用print\r($SmallBox),假设显示警告“因为echo无法接受数组,打开php的错误报告 如果要格式化,请执行以下操作:
print "<pre>";
print_r($SmallBox);
print "</pre>";
exit;
print”“;
// I've separated them into lines for easier readability
$boxes = array(
"small" => array("length" =>12, "width" =>10, "depth" =>2.5),
"medium" => array("length" =>30, "width" =>20, "depth" =>4),
"large" => array("length" =>60, "width" =>40, "depth" =>11.5)
);
// Show the structure and contents of the array
echo '<h2>Array Structure</h2>';
print_r($boxes);
// You will have to access these elements individually
// Small box:
echo '<h2>Small Box</h2>';
echo 'Length: '.$boxes['small']["length"].'<br>';
echo 'Width: '.$boxes['small']["width"].'<br>';
echo 'Depth: '.$boxes['small']["depth"].'<br>';
echo 'Volume: '.$boxes['small']["length"]*$boxes['small']["width"]*$boxes['small']["depth"].'<br>';
// Medium box:
echo '<h2>MediumBox</h2>';
echo 'Length: '.$boxes['medium']["length"].'<br>';
echo 'Width: '.$boxes['medium']["width"].'<br>';
echo 'Depth: '.$boxes['medium']["depth"].'<br>';
echo 'Volume: '.$boxes['medium']["length"]*$boxes['medium']["width"]*$boxes['medium']["depth"].'<br>';
打印(小盒子);
打印“”;
出口
要了解数据类型和长度,请使用var_dump($SmallBox)
要打开错误报告,请参阅本文:您定义的数组是一维关联数组 应改为使用此方法定义框数组:
//为了便于阅读,我将它们分成了几行
$box=数组(
“小”=>阵列(“长度”=>12,“宽度”=>10,“深度”=>2.5),
“中等”=>阵列(“长度”=>30,“宽度”=>20,“深度”=>4),
“大”=>阵列(“长度”=>60,“宽度”=>40,“深度”=>11.5)
);
//显示数组的结构和内容
回波“阵列结构”;
印刷品(盒);
//您必须单独访问这些元素
//小盒子:
回声“小盒子”;
回显“长度:”.$box[“小”][“长度”]。
;
回显“宽度:”.$box[“小”][“宽度”]。
;
回声“深度:”.$box[“小”][“深度”]。
;
回声“音量:”.$box['small'][“长度”]*$box['small'][“宽度”]*$box['small'][“深度”]。
;
//中盒:
echo‘MediumBox’;
回显“长度:”.$box[“中等”][“长度”]。
;
回声“宽度:”.$box[“中等”][“宽度”]。
;
回声“深度:”.$box[“中等”][“深度”]。
;
回显“音量:”.$box['medium'][“长度”]*$box['medium'][“宽度”]*$box['medium'][“深度”]。
;
打开wamp错误报告以显示警告。当我使用您的方法时,返回“数组([小]=>数组([长]=>12[宽]=>10[深]=>2.5)[中]=>数组([长]=>30[宽]=>20[深]=>4)[大]=>数组([长]=>60[宽]=>40[深]=>11.5])12102.530204”当我通过WAMP执行脚本时。@user5374923“是”,这是正确的预期输出。它显示多维关联数组。下面的数字是您回显出来的单个数字(回显不会在末尾追加新行。您需要自己连接新行,在HTML中是。回显$box['small'][“length”];然后需要更改为回显$box['small'][“length”]“
”;我将更新代码示例以显示此内容。啊,我明白了。非常感谢您澄清此问题!@user5374923干杯!如果您认为我的答案相关,请标记:)
// I've separated them into lines for easier readability
$boxes = array(
"small" => array("length" =>12, "width" =>10, "depth" =>2.5),
"medium" => array("length" =>30, "width" =>20, "depth" =>4),
"large" => array("length" =>60, "width" =>40, "depth" =>11.5)
);
// Show the structure and contents of the array
echo '<h2>Array Structure</h2>';
print_r($boxes);
// You will have to access these elements individually
// Small box:
echo '<h2>Small Box</h2>';
echo 'Length: '.$boxes['small']["length"].'<br>';
echo 'Width: '.$boxes['small']["width"].'<br>';
echo 'Depth: '.$boxes['small']["depth"].'<br>';
echo 'Volume: '.$boxes['small']["length"]*$boxes['small']["width"]*$boxes['small']["depth"].'<br>';
// Medium box:
echo '<h2>MediumBox</h2>';
echo 'Length: '.$boxes['medium']["length"].'<br>';
echo 'Width: '.$boxes['medium']["width"].'<br>';
echo 'Depth: '.$boxes['medium']["depth"].'<br>';
echo 'Volume: '.$boxes['medium']["length"]*$boxes['medium']["width"]*$boxes['medium']["depth"].'<br>';