我已经被遗忘了一段时间,并且想知道是否有办法将这些数据转换为自定义多维数组。
我在我的桌子上运行一个查询,它会像这样吐出一个数组
SELECT id_cms_category, name FROM cms_category_lang ORDER BY id_cms_category ASC
结果数组看起来像这样。
array
0=>
array
'id_cms_category'=> 1
'name'=>'Home'
1=>
array
'id_cms_category'=> 2
'name'=>'Test'
然后我在我的cms表上运行我的查询
SELECT cl.id_cms, c.id_cms_category, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci ON (ci.id_cms = cl.id_cms)
ORDER BY c.id_cms_category, cl.meta_title ASC
我得到一个像这样的数组
array
0 =>
array
'id_cms' => string '4' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'About us' (length=8)
1 =>
array
'id_cms' => string '6' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Contact Us' (length=10)
2 =>
array
'id_cms' => string '1' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Delivery' (length=8)
3 =>
array
'id_cms' => string '2' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Legal Notice' (length=12)
4 =>
array
'id_cms' => string '5' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Secure payment' (length=14)
5 =>
array
'id_cms' => string '3' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Terms and conditions of use' (length=27)
6 =>
array
'id_cms' => string '10' (length=2)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech IBC Spill Pallet Plus' (length=37)
7 =>
array
'id_cms' => string '9' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech P2 Plus 2-Drum Hard Top Spill Pallet' (length=52)
8 =>
array
'id_cms' => string '7' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'XR-5, Urethane & Copolymer 2000' (length=31)
我想尝试获取所有这些数据并以某种方式创建一个多维数组;
array
0 => 'Home'
array
id_cms => meta_title
1 => 'Test'
array
id_cms => meta_title
这样我就能运行这样的东西
foreach($title as $key => $value)
{
<li>$value</li>
foreach($value as $id_cms => $page)
{
<li><a href="$id_cms">$page</a></li>
}
}
让页面上的输出看起来像这样
Home
home 1
home 2
home 3
Test
test 1
test 2
test 3
答案 0 :(得分:1)
我不会对此进行两次查询。
我会更改您的第二个查询,因此您在同一结果集中同时拥有类别和页面名称。它应该看起来像:
SELECT cl.id_cms, c.id_cms_category, catlang.name, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c
ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci
ON (ci.id_cms = cl.id_cms)
LEFT JOIN cms_category_lang catlang
ON (catlang.id_cms_category = cl.id_cms_category)
ORDER BY c.id_cms_category, catlang.name, cl.meta_title ASC;
然后你可以创建一个循环,只需在每次迭代中检查类别。如果找到新的,则结束前一个块并开始一个新块:
$currentCategory = '';
$output = '';
foreach ($rows as $row) {
if ($currentCategory != $row['name']) {
$output .= '</li><li>' . $row['name'];
}
$currentCategory = $row['name'];
$output .= '<li><a href="' . $row['id_cms'] . '">' . $row['meta_title'] . '</li>';
}
// Trim off our unnecessary </li>
$output = substr($output, 4);
答案 1 :(得分:0)
如果您的CMS返回一个数字索引数组(这就是您的帖子中的声音),您可以使用您想要的任何键重新索引数组。
$array = array(0 => array('id_cms' => 4, 'id_cms_category' => 1, 'meta_title' => 'About us'));
$newArray = array();
foreach ( $array as $v ) {
$newArray[$v['meta_title']] = $v;
}
var_dump($newArray);
数组在PHP编程中至关重要,因此在继续学习之前,你可以帮助他们学习它们。
答案 2 :(得分:-1)
$newArray = array();
if (isset($newArray[$row['id_cms_category']))
$newArray[$row['id_cms_category'] = array($row);
$newArray[$row['id_cms_category'] []= $row;
就是这样!