关联数组中的关联数组
Associative Array within Associative Array
我是 PHP 的新手,正在尝试在命名数组中应用命名数组。我已经能够应用每个绳索名称(即 "Poly Soak")以及 img、title、blurb、desc 等,但我很难将 $specTable
应用到 table .我也在尝试申请 $applications
和 $benefits
但没有成功。
我正在使用 GET 命令生成每个页面。页面填充如下:
water-rescue-ropes.php?value=aqua-d
water-rescue-ropes.php?value=poly-soak...等
function strip_bad_chars( $input ) {
$output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
return $output; }
if (isset($_GET['value'])) {
$waterRescueRope = strip_bad_chars( $_GET['value'] );
$rope = $waterRescueRopes[$waterRescueRope];}
这是编辑的数组信息:
$waterRescueRopes = array(
"poly-soak" => array(
'img' => "3P",
'title' => "Poly Soak™",
'blurb' => "Pelican’s 16 strand kernmantle construction is a...",
'desc' => "Polypropylene Water Rescue Rope",
'construction' => "16 Strand",
'cover' => "Polypropylene",
'core' => "Polypropylene",
array(
array(
'sku' => "3P-12Y",
'diameter' => "3/8"",
'tensile' => 3000,
'weight' => 3.4,
),
array(
'sku' => "3P-14Y",
'diameter' => "7/16"",
'tensile' => 3380,
'weight' => 4.7,
),
),
array("Water Rescue", "Canyoneering"),
array("Lightweight", "Buoyant", "High Visibility"),
),
"poly-soak-economy" => array(
'img' => "FQK",
'title' => "Poly Soak™ Economy",
'blurb' => "All the great benefits and features of our Poly Soak rope but at a lower cost.",
'desc' => "Polypropylene Economy Water Rescue Rope",
'construction' => "16 Strand",
'cover' => "Polypropylene",
'core' => "Polypropylene",
array(
array(
'sku' => "FQK-502",
'diameter' => "1/8"",
'tensile' => 2000,
'weight' => 1.5,
),
),
array("Water Rescue"),
array("Economical", "Lightweight", "Buoyant"),
),
"poly-soak-2" => array(
'img' => "3PN",
'title' => "Poly Soak™",
'blurb' => "This UL certified polypropylene rope is approved for...",
'desc' => "NFPA® Water Rescue Rope",
'construction' => "24 Strand",
'cover' => "Polypropylene",
'core' => "Polypropylene",
array(
array(
'sku' => "3PN-12Y",
'diameter' => "3/8"",
'tensile' => 4200,
'weight' => 3.05,
),
),
array("Water Rescue", "Canyoneering", "Mooring"),
array("Bright Colors for Greater Visibility", "Supple for Compact Storage", "Certified to the Highest Standard", "Buoyant"),
),
"aqua-d" => array(
'img' => "3q",
'title' => "Aqua-D™",
'blurb' => "This water rescue rope has a blended Dyneema® and polypropylene core. With a multifilament polypropylene cover, this rope floats and has near zero water retention. It is extremely strong, light 'weight', and has a no-stretch braid that meets the needs of coastal and river rescue professionals worldwide.",
'desc' => "Dyneema® Polypropylene Water Rescue Rope",
'construction' => "16 Strand",
'cover' => "Polypropylene",
'core' => "Dyneema®",
array(
array(
'sku' => "3Q-08Y",
'diameter' => "1/4"",
'tensile' => "3200",
'weight' => "1.7",
),
array(
'sku' => "3Q-10Y",
'diameter' => "5/16"",
'tensile' => "3400",
'weight' => "2.3",
),
array(
'sku' => "3Q-12Y",
'diameter' => "3/8"",
'tensile' => "4950",
'weight' => "3.4",
),
),
array("Minimal Water Retention", "Strong & Lightweight", "Buoyant"),
array("Water Rescue"),
), );
额外包含的功能:
function get_specs($array = false)
{
if (!is_array($array))
return array();
elseif(empty($array))
return array();
foreach($array as $polytype => $polydata) {
$new[$polytype]['specs'] = (!empty($polydata[0]))? $polydata[0]:array();
$new[$polytype]['applications'] = (!empty($polydata[1]))? $polydata[1]:array();
$new[$polytype]['benefits'] = (!empty($polydata[2]))? $polydata[2]:array();
}
return $new;
}
这是编辑的 规格表:
<?php function display_specs($spec = array(),$name = false)
{
if(!empty($spec[$name]['specs'])) {
ob_start();
foreach($spec[$name]['specs'] as $specs) {
?>
<tr>
<td><?php echo $specs['sku']; ?></td>
<td><?php echo $specs['diameter']; ?></td>
<td><?php echo $specs['tensile']; ?></td>
<td><?php echo $specs['weight']; ?></td>
</tr>
<?php
}
$data = ob_get_contents();
}
} ?>
// Isolates the specs from the main array
$specs = get_specs($waterRescueRopes);
// Alternately, you can loop through the main array and get all
<?php foreach($specs as $polytype => $spec) {
echo display_specs($specs,$_GET['value']);
}
?>
已编辑 应用程序(好处在类似列表中):
<?php function display_applications($spec = array(),$name = false)
{
if(!empty($spec[$name]['applications'])) {
ob_start();
foreach($spec[$name]['applications'] as $apps) {
?>
<li><?php echo $apps; ?></li>
<?php
}
$data = ob_get_contents();
return $data;
}
}
?>
<tr valign='top'>
<td class="td-item-cell"><article>
<div class="rel_product_wrapper">
<h3>Applications</h3>
<ul>
<li>
<?php
foreach($specs as $polytype => $spec) {
echo display_applications($specs,$_GET['value']);
}
?>
</li>
</ul>
</div>
</article></td>
</tr>
我目前让它们填充我需要的东西但多次(我假设四种类型的绳子有四次)。
感谢您的宝贵时间!我很感激! :)
删除数组中的变量,然后创建一个函数来单独提取您需要的那些方面。你所做的事情不起作用的原因是因为每次你在每个数组上写 $specTable
时,你 re-assign/overwrite 先前分配的 $specTable
到当前数组,直到最后它被分配到最后一个主数组中的数组。这就是为什么只有一个出现。您需要将这些方面保存到新数组中:
FUNCTIONS: 将函数放在页面顶部或放在您包含在此页面上以供使用的不同文件中。最好包括使用 include("function.specs.php")
之类的内容,因为这样您就可以根据需要在不同页面上重复使用这些功能。
function get_specs($array = false)
{
if (!is_array($array))
return array();
elseif(empty($array))
return array();
foreach($array as $polytype => $polydata) {
$new[$polytype]['specs'] = (!empty($polydata[0]))? $polydata[0]:array();
$new[$polytype]['applications'] = (!empty($polydata[1]))? $polydata[1]:array();
$new[$polytype]['benefits'] = (!empty($polydata[2]))? $polydata[2]:array();
}
return $new;
}
function display_specs($spec = array(),$name = false)
{
if(!empty($spec[$name]['specs'])) {
ob_start();
foreach($spec[$name]['specs'] as $specs) {
?>
<tr>
<td><?php echo $specs['sku']; ?></td>
<td><?php echo $specs['diameter']; ?></td>
<td><?php echo $specs['tensile']; ?></td>
<td><?php echo $specs['weight']; ?></td>
</tr>
<?php
}
$data = ob_get_contents();
return $data;
}
}
// For sake of ease, you can do an application one by itself,
// Normally, you could just do one class/method that handles all the
// the different arrays
function display_applications($spec = array(),$name = false)
{
if(!empty($spec[$name]['applications'])) {
ob_start();
foreach($spec[$name]['applications'] as $apps) {
?>
<tr>
<td><?php echo $apps; ?></td>
</tr>
<?php
}
$data = ob_get_contents();
return $data;
}
}
使用您页面上的功能:
// Isolates the specs from the main array
$specs = get_specs($waterRescueRopes);
// To select one product out of the bunch, just use the name from the array.
// Note, you need the $specs array as the first argument
echo display_specs($specs,"poly-soak");
echo display_applications($specs,"poly-soak");
// Alternately, you can loop through the main array and get all: ?>
<table>
<?php
foreach($specs as $polytype => $spec) {
echo display_specs($specs,$polytype);
echo display_applications($specs,$polytype);
}
?>
</table>
应用程序部分:因为函数已经为你做了循环,你只需要回显它:
<tr valign='top'>
<td class="td-item-cell"><article>
<div class="rel_product_wrapper">
<h3>Applications</h3>
<ul>
<li>
<?php echo display_applications($specs,$_GET['value']); ?>
</li>
</ul>
</div>
</article></td>
</tr>
RAW print_r($specs) 给你:
Array
(
[poly-soak] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => 3P-12Y
[diameter] => 3/8"
[tensile] => 3000
[weight] => 3.4
)
[1] => Array
(
[sku] => 3P-14Y
[diameter] => 7/16"
[tensile] => 3380
[weight] => 4.7
)
)
[applications] => Array
(
[0] => Water Rescue
[1] => Canyoneering
)
[benefits] => Array
(
[0] => Lightweight
[1] => Buoyant
[2] => High Visibility
)
)
[poly-soak-economy] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => FQK-502
[diameter] => 1/8"
[tensile] => 2000
[weight] => 1.5
)
)
[applications] => Array
(
[0] => Water Rescue
)
[benefits] => Array
(
[0] => Economical
[1] => Lightweight
[2] => Buoyant
)
)
[poly-soak-2] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => 3PN-12Y
[diameter] => 3/8"
[tensile] => 4200
[weight] => 3.05
)
)
[applications] => Array
(
[0] => Water Rescue
[1] => Canyoneering
[2] => Mooring
)
[benefits] => Array
(
[0] => Bright Colors for Greater Visibility
[1] => Supple for Compact Storage
[2] => Certified to the Highest Standard
[3] => Buoyant
)
)
[aqua-d] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => 3Q-08Y
[diameter] => 1/4"
[tensile] => 3200
[weight] => 1.7
)
[1] => Array
(
[sku] => 3Q-10Y
[diameter] => 5/16"
[tensile] => 3400
[weight] => 2.3
)
[2] => Array
(
[sku] => 3Q-12Y
[diameter] => 3/8"
[tensile] => 4950
[weight] => 3.4
)
)
[applications] => Array
(
[0] => Minimal Water Retention
[1] => Strong & Lightweight
[2] => Buoyant
)
[benefits] => Array
(
[0] => Water Rescue
)
)
)
我是 PHP 的新手,正在尝试在命名数组中应用命名数组。我已经能够应用每个绳索名称(即 "Poly Soak")以及 img、title、blurb、desc 等,但我很难将 $specTable
应用到 table .我也在尝试申请 $applications
和 $benefits
但没有成功。
我正在使用 GET 命令生成每个页面。页面填充如下:
water-rescue-ropes.php?value=aqua-d
water-rescue-ropes.php?value=poly-soak...等
function strip_bad_chars( $input ) {
$output = preg_replace( "/[^a-zA-Z0-9_-]/", "", $input );
return $output; }
if (isset($_GET['value'])) {
$waterRescueRope = strip_bad_chars( $_GET['value'] );
$rope = $waterRescueRopes[$waterRescueRope];}
这是编辑的数组信息:
$waterRescueRopes = array(
"poly-soak" => array(
'img' => "3P",
'title' => "Poly Soak™",
'blurb' => "Pelican’s 16 strand kernmantle construction is a...",
'desc' => "Polypropylene Water Rescue Rope",
'construction' => "16 Strand",
'cover' => "Polypropylene",
'core' => "Polypropylene",
array(
array(
'sku' => "3P-12Y",
'diameter' => "3/8"",
'tensile' => 3000,
'weight' => 3.4,
),
array(
'sku' => "3P-14Y",
'diameter' => "7/16"",
'tensile' => 3380,
'weight' => 4.7,
),
),
array("Water Rescue", "Canyoneering"),
array("Lightweight", "Buoyant", "High Visibility"),
),
"poly-soak-economy" => array(
'img' => "FQK",
'title' => "Poly Soak™ Economy",
'blurb' => "All the great benefits and features of our Poly Soak rope but at a lower cost.",
'desc' => "Polypropylene Economy Water Rescue Rope",
'construction' => "16 Strand",
'cover' => "Polypropylene",
'core' => "Polypropylene",
array(
array(
'sku' => "FQK-502",
'diameter' => "1/8"",
'tensile' => 2000,
'weight' => 1.5,
),
),
array("Water Rescue"),
array("Economical", "Lightweight", "Buoyant"),
),
"poly-soak-2" => array(
'img' => "3PN",
'title' => "Poly Soak™",
'blurb' => "This UL certified polypropylene rope is approved for...",
'desc' => "NFPA® Water Rescue Rope",
'construction' => "24 Strand",
'cover' => "Polypropylene",
'core' => "Polypropylene",
array(
array(
'sku' => "3PN-12Y",
'diameter' => "3/8"",
'tensile' => 4200,
'weight' => 3.05,
),
),
array("Water Rescue", "Canyoneering", "Mooring"),
array("Bright Colors for Greater Visibility", "Supple for Compact Storage", "Certified to the Highest Standard", "Buoyant"),
),
"aqua-d" => array(
'img' => "3q",
'title' => "Aqua-D™",
'blurb' => "This water rescue rope has a blended Dyneema® and polypropylene core. With a multifilament polypropylene cover, this rope floats and has near zero water retention. It is extremely strong, light 'weight', and has a no-stretch braid that meets the needs of coastal and river rescue professionals worldwide.",
'desc' => "Dyneema® Polypropylene Water Rescue Rope",
'construction' => "16 Strand",
'cover' => "Polypropylene",
'core' => "Dyneema®",
array(
array(
'sku' => "3Q-08Y",
'diameter' => "1/4"",
'tensile' => "3200",
'weight' => "1.7",
),
array(
'sku' => "3Q-10Y",
'diameter' => "5/16"",
'tensile' => "3400",
'weight' => "2.3",
),
array(
'sku' => "3Q-12Y",
'diameter' => "3/8"",
'tensile' => "4950",
'weight' => "3.4",
),
),
array("Minimal Water Retention", "Strong & Lightweight", "Buoyant"),
array("Water Rescue"),
), );
额外包含的功能:
function get_specs($array = false)
{
if (!is_array($array))
return array();
elseif(empty($array))
return array();
foreach($array as $polytype => $polydata) {
$new[$polytype]['specs'] = (!empty($polydata[0]))? $polydata[0]:array();
$new[$polytype]['applications'] = (!empty($polydata[1]))? $polydata[1]:array();
$new[$polytype]['benefits'] = (!empty($polydata[2]))? $polydata[2]:array();
}
return $new;
}
这是编辑的 规格表:
<?php function display_specs($spec = array(),$name = false)
{
if(!empty($spec[$name]['specs'])) {
ob_start();
foreach($spec[$name]['specs'] as $specs) {
?>
<tr>
<td><?php echo $specs['sku']; ?></td>
<td><?php echo $specs['diameter']; ?></td>
<td><?php echo $specs['tensile']; ?></td>
<td><?php echo $specs['weight']; ?></td>
</tr>
<?php
}
$data = ob_get_contents();
}
} ?>
// Isolates the specs from the main array
$specs = get_specs($waterRescueRopes);
// Alternately, you can loop through the main array and get all
<?php foreach($specs as $polytype => $spec) {
echo display_specs($specs,$_GET['value']);
}
?>
已编辑 应用程序(好处在类似列表中):
<?php function display_applications($spec = array(),$name = false)
{
if(!empty($spec[$name]['applications'])) {
ob_start();
foreach($spec[$name]['applications'] as $apps) {
?>
<li><?php echo $apps; ?></li>
<?php
}
$data = ob_get_contents();
return $data;
}
}
?>
<tr valign='top'>
<td class="td-item-cell"><article>
<div class="rel_product_wrapper">
<h3>Applications</h3>
<ul>
<li>
<?php
foreach($specs as $polytype => $spec) {
echo display_applications($specs,$_GET['value']);
}
?>
</li>
</ul>
</div>
</article></td>
</tr>
我目前让它们填充我需要的东西但多次(我假设四种类型的绳子有四次)。
感谢您的宝贵时间!我很感激! :)
删除数组中的变量,然后创建一个函数来单独提取您需要的那些方面。你所做的事情不起作用的原因是因为每次你在每个数组上写 $specTable
时,你 re-assign/overwrite 先前分配的 $specTable
到当前数组,直到最后它被分配到最后一个主数组中的数组。这就是为什么只有一个出现。您需要将这些方面保存到新数组中:
FUNCTIONS: 将函数放在页面顶部或放在您包含在此页面上以供使用的不同文件中。最好包括使用 include("function.specs.php")
之类的内容,因为这样您就可以根据需要在不同页面上重复使用这些功能。
function get_specs($array = false)
{
if (!is_array($array))
return array();
elseif(empty($array))
return array();
foreach($array as $polytype => $polydata) {
$new[$polytype]['specs'] = (!empty($polydata[0]))? $polydata[0]:array();
$new[$polytype]['applications'] = (!empty($polydata[1]))? $polydata[1]:array();
$new[$polytype]['benefits'] = (!empty($polydata[2]))? $polydata[2]:array();
}
return $new;
}
function display_specs($spec = array(),$name = false)
{
if(!empty($spec[$name]['specs'])) {
ob_start();
foreach($spec[$name]['specs'] as $specs) {
?>
<tr>
<td><?php echo $specs['sku']; ?></td>
<td><?php echo $specs['diameter']; ?></td>
<td><?php echo $specs['tensile']; ?></td>
<td><?php echo $specs['weight']; ?></td>
</tr>
<?php
}
$data = ob_get_contents();
return $data;
}
}
// For sake of ease, you can do an application one by itself,
// Normally, you could just do one class/method that handles all the
// the different arrays
function display_applications($spec = array(),$name = false)
{
if(!empty($spec[$name]['applications'])) {
ob_start();
foreach($spec[$name]['applications'] as $apps) {
?>
<tr>
<td><?php echo $apps; ?></td>
</tr>
<?php
}
$data = ob_get_contents();
return $data;
}
}
使用您页面上的功能:
// Isolates the specs from the main array
$specs = get_specs($waterRescueRopes);
// To select one product out of the bunch, just use the name from the array.
// Note, you need the $specs array as the first argument
echo display_specs($specs,"poly-soak");
echo display_applications($specs,"poly-soak");
// Alternately, you can loop through the main array and get all: ?>
<table>
<?php
foreach($specs as $polytype => $spec) {
echo display_specs($specs,$polytype);
echo display_applications($specs,$polytype);
}
?>
</table>
应用程序部分:因为函数已经为你做了循环,你只需要回显它:
<tr valign='top'>
<td class="td-item-cell"><article>
<div class="rel_product_wrapper">
<h3>Applications</h3>
<ul>
<li>
<?php echo display_applications($specs,$_GET['value']); ?>
</li>
</ul>
</div>
</article></td>
</tr>
RAW print_r($specs) 给你:
Array
(
[poly-soak] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => 3P-12Y
[diameter] => 3/8"
[tensile] => 3000
[weight] => 3.4
)
[1] => Array
(
[sku] => 3P-14Y
[diameter] => 7/16"
[tensile] => 3380
[weight] => 4.7
)
)
[applications] => Array
(
[0] => Water Rescue
[1] => Canyoneering
)
[benefits] => Array
(
[0] => Lightweight
[1] => Buoyant
[2] => High Visibility
)
)
[poly-soak-economy] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => FQK-502
[diameter] => 1/8"
[tensile] => 2000
[weight] => 1.5
)
)
[applications] => Array
(
[0] => Water Rescue
)
[benefits] => Array
(
[0] => Economical
[1] => Lightweight
[2] => Buoyant
)
)
[poly-soak-2] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => 3PN-12Y
[diameter] => 3/8"
[tensile] => 4200
[weight] => 3.05
)
)
[applications] => Array
(
[0] => Water Rescue
[1] => Canyoneering
[2] => Mooring
)
[benefits] => Array
(
[0] => Bright Colors for Greater Visibility
[1] => Supple for Compact Storage
[2] => Certified to the Highest Standard
[3] => Buoyant
)
)
[aqua-d] => Array
(
[specs] => Array
(
[0] => Array
(
[sku] => 3Q-08Y
[diameter] => 1/4"
[tensile] => 3200
[weight] => 1.7
)
[1] => Array
(
[sku] => 3Q-10Y
[diameter] => 5/16"
[tensile] => 3400
[weight] => 2.3
)
[2] => Array
(
[sku] => 3Q-12Y
[diameter] => 3/8"
[tensile] => 4950
[weight] => 3.4
)
)
[applications] => Array
(
[0] => Minimal Water Retention
[1] => Strong & Lightweight
[2] => Buoyant
)
[benefits] => Array
(
[0] => Water Rescue
)
)
)