我正在尝试创建一个多维数组,其中包含一个包含一些信息的索引,这些项应该链接到它各自的索引。
类似的东西:
Category-01 (Image, url1, url2)
- 01 Product (name, url1, url2, url3)
- 02 Product (name, url1, url2, url3)
- 03 Product (name, url1, url2, url3)
Category-02 (Image, url1, url2)
- 04 Product (name, url1, url2, url3)
- 05 Product (name, url1, url2, url3)
- 06 Product (name, url1, url2, url3)
我到目前为止(更改我用作基础的文件)是:
<?php
$categories = array(
"01" => array(
array("name" => "01", "mirror1" => "#URL011", "mirror2" => "#URL021", "mirror3" => "#URL031",),
array("name" => "02", "mirror1" => "#URL012", "mirror2" => "#URL022", "mirror3" => "#URL032",),
array("name" => "03", "mirror1" => "#URL013", "mirror2" => "#URL023", "mirror3" => "#URL033",),
),
"02" => array(
array("name" => "04", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
array("name" => "05", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
),
"03" => array(
array("name" => "06", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
array("name" => "07", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
),
);
// Setup the preprocessing.
$numColumns = 99;
$columnLength = array();
$columnData = array();
for ($i = 0; $i <= $numColumns; $i++)
{ $columnLength[] = 0; $columnData[] = ''; }
// Sort the category array
ksort($categories);
// Process our data
foreach ($categories as $cname => $subcats) {
$minLength = $columnLength[0];
$minIndex = 0;
for ($i = 1; $i < $numColumns; $i++) {
if ($columnLength[$i] < $minLength) {
$minLength = $columnLength[$i];
$minIndex = $i;
}
}
$columnLength[$minIndex] += 1 + count($subcat);
$columnData[$minIndex] .= '- Categoria '.$cname;
foreach($subcats as $subcat) {
$columnData[$minIndex] .= 'Produto '.$subcat['name'].' | [<a title="Produto URL01.'.$subcat['name'].'" href="'.$subcat['mirror1'].'">URL01</a>] - [<a title="Produto URL02.'.$subcat['name'].'" href="'.$subcat['mirror2'].'">URL02</a>] - [<a title="Produto URL03.'.$subcat['name'].'" href="'.$subcat['mirror3'].'">URL03</a>]<br/>';
};
};
// Display our data
for ($i = 0; $i < $numColumns; $i++) {
echo $columnData[$i];
}
?>
PS:我还在学习,我是php的新手。
我会做的是这样的事情:
$categories = array(
"Cat" => "01", "Image" => "image_url", "cat_link1" => "#CATURL01", "cat_link2" => "#CATURL02" => array(
array("name" => "01", "mirror1" => "#URL011", "mirror2" => "#URL021", "mirror3" => "#URL031",),
array("name" => "02", "mirror1" => "#URL012", "mirror2" => "#URL022", "mirror3" => "#URL032",),
array("name" => "03", "mirror1" => "#URL013", "mirror2" => "#URL023", "mirror3" => "#URL033",),
),
"Cat" => "02", "Image" => "image_url", "cat_link1" => "#CATURL01", "cat_link2" => "#CATURL02" => array(
array("name" => "04", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
array("name" => "05", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
),
"Cat" => "03", "Image" => "image_url", "cat_link1" => "#CATURL01", "cat_link2" => "#CATURL02" => array(
array("name" => "06", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
array("name" => "07", "mirror1" => "#URL01", "mirror2" => "#URL02", "mirror3" => "#URL03",),
),
);
如果可以,请将结果显示为:
Category-01 (Image)
- 01 Product (name, url1, url2, url3)
- 02 Product (name, url1, url2, url3)
- 03 Product (name, url1, url2, url3)
catURL1, catURL2
Category-02 (Image)
- 04 Product (name, url1, url2, url3)
- 05 Product (name, url1, url2, url3)
- 06 Product (name, url1, url2, url3)
catURL1, catURL2
但是脚本不能正常工作,它会显示产品和信息,但不能将信息放在类别/索引上而不是标题上。
提前感谢您的帮助。
根据inti。
提出的修订<?php
$categories = array(
"01" => array(
"Image" => "product01.jpg",
"url1" => "SiteProduct",
"url2" => "SiteSupport",
"Products" => array(
array("name" => "01", "url1" => "Site1.1", "url2" => "Site1.2", "url3" => "Site1.3"),
array("name" => "02", "url1" => "Site2.1", "url2" => "Site2.2", "url3" => "Site2.3"),
array("name" => "03", "url1" => "Site3.1", "url2" => "Site3.2", "url3" => "Site3.3"),
array("name" => "04", "url1" => "Site4.1", "url2" => "Site4.2", "url3" => "Site4.3"),
// and so on the Products...
)
),// and so on the Categories...
"02" => array(
"Image" => "product02.jpg",
"url1" => "SiteProduct",
"url2" => "SiteSupport",
"Products" => array(
array("name" => "05", "url1" => "Site5.1", "url2" => "Site5.2", "url3" => "Site5.3"),
array("name" => "06", "url1" => "Site6.1", "url2" => "Site6.2", "url3" => "Site6.3"),
array("name" => "07", "url1" => "Site7.1", "url2" => "Site7.2", "url3" => "Site7.3"),
array("name" => "08", "url1" => "Site8.1", "url2" => "Site8.2", "url3" => "Site8.3"),
// and so on the Products...
)
),
);
// Setup the preprocessing.
$numColumns = 2;
$columnLength = array();
$columnData = array();
for ($i = 0; $i <= $numColumns; $i++)
{ $columnLength[] = 0; $columnData[] = ''; }
// Sort the category array
ksort($categories);
// Process our data
foreach ($categories as $cname => $cat) {
$minLength = $columnLength[0];
$minIndex = 0;
for ($i = 1; $i < $numColumns; $i++) {
if ($columnLength[$i] < $minLength) {
$minLength = $columnLength[$i];
$minIndex = $i;
}
}
$columnLength[$minIndex] += 1 + count($cat);
$columnData[$minIndex] .= '<p>'.$cname."</p>\n";
foreach($cat['Products'] as $subcat) {
$columnData[$minIndex] .= 'Product '.$subcat['name'].' - '.$subcat['url1'].' - '.$subcat['url2'].' - '.$subcat['url3']."<br/>\n";
}
}
// Display our data
for ($i = 0; $i < $numColumns; $i++) {
echo '<div class="column'.($i+1).'">'.$columnData[$i]."</div>\n";
}
?>
答案 0 :(得分:2)
您必须通过向每个类别添加Products值,将产品嵌套在类别中。它将包含产品列表。数组看起来像这样:
$categories = array(
"01" => array(
"Image" => "...",
"url1" => "...",
"url2" => "...",
"Products" => array(
array("name" => "...", "url1" => "...", "url2" => "...", "url3" => "..."),
// and so on the Products...
)
),
// and so on the Categories...
);
此数组的处理方式略有不同,但请注意foreach
如何迭代$cat['Products']
:
foreach ($categories as $cname => $cat)
{
// here you raech the data of the category by $cat ...
foreach ($cat['Products'] as $subcat)
{
// here you reach the data of the product by $subcat ...
}
}