大家好我需要一些关于提取数组的帮助,下面是我的数组:
Array
(
[11] => Array
(
[category_id] => 11
[parent_id] => 1
[id_path] => 1/11
[category] => Filipino
[position] => 10
[status] => A
[seo_name] => filipino-en
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 1
[subcategories] => Array
(
[0] => Array
(
[category_id] => 42
[parent_id] => 11
[id_path] => 1/11/42
[category] => Adobo
[position] => 10
[status] => A
[seo_name] => adobo
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 2
)
[1] => Array
(
[category_id] => 43
[parent_id] => 11
[id_path] => 1/11/43
[category] => Sinigang
[position] => 20
[status] => A
[seo_name] => singang
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 2
)
[2] => Array
(
[category_id] => 44
[parent_id] => 11
[id_path] => 1/11/44
[category] => Kare-Kare
[position] => 30
[status] => A
[seo_name] => kare-kare
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 2
)
[3] => Array
(
[category_id] => 45
[parent_id] => 11
[id_path] => 1/11/45
[category] => Dinuguan
[position] => 40
[status] => A
[seo_name] => dinuguan-en
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 2
)
[4] => Array
(
[category_id] => 4
[parent_id] => 1
[id_path] => 1/4
[category] => Continental
[position] => 20
[status] => A
[seo_name] => continental
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 1
[subcategories] => Array
(
[0] => Array
(
[category_id] => 12
[parent_id] => 4
[id_path] => 1/4/12
[category] => American
[position] => 10
[status] => A
[seo_name] => american
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 2
[subcategories] => Array
(
[0] => Array
(
[category_id] => 54
[parent_id] => 12
[id_path] => 1/4/12/54
[category] => Burger
[position] => 10
[status] => A
[seo_name] => burger
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 3
)
[1] => Array
(
[category_id] => 55
[parent_id] => 12
[id_path] => 1/4/12/55
[category] => Steak
[position] => 20
[status] => A
[seo_name] => steak-en
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 3
)
[2] => Array
(
[category_id] => 56
[parent_id] => 12
[id_path] => 1/4/12/56
[category] => Turkey
[position] => 30
[status] => A
[seo_name] => turkey
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 3
)
[3] => Array
(
[category_id] => 57
[parent_id] => 12
[id_path] => 1/4/12/57
[category] => Salad
[position] => 40
[status] => A
[seo_name] => salad
[age_verification] => N
[age_limit] => 0
[age_warning_message] =>
[level] => 3
)
如何使用以下格式提取数组:
-Filipino
- Adobo
- Sinigang
- Kare Kare
-Continental
- American
- Burger
- Steak
- Turkey
- Salad
我有这段代码:
foreach($categories_tree as $k=>$v){
if($v.subcategories){
echo "<div class=\"w900 h024 ";
echo $v['level'] == 1 ? "ml024" : "ml048";
echo '" >'."\n";
echo "<a href=\"\" class=\"w010 h010 p007 b03 d2 f1\">\n";
echo "<div class=\"it3 ib3 il3 jt05 jb05 jl10 kt04 kb04 kl03\"></div>\n";
echo "</a>\n";
echo "<div class=\"tw2 w839 h012 pt006 pb005 pl012 b04 ir3 ib3 jr01 jb01 kr02 kb02 f1\">\n";
echo "<input type=\"checkbox\" class=\"f1 w012 h012 m000 f1\" value=\"".$v['category_id']."\" />\n";
echo "<p class=\"ml012 f1\">".$v['category']."</p>\n";
echo "<div class=\"r3\"></div>\n";
echo "</div>\n";
echo "<div class=\"r3\"></div>\n";
echo "</div>\n";
//echo $v;
}else{
}
}
$ categories_tree是我的主要数组。 现在,如果我运行我的代码,它将显示:
- Filipino
- Continental
- Asian
- Others
但我想创建一个子类别。我怎么能这样做?
答案 0 :(得分:3)
使用array_walk_recursive循环嵌套数组
function output_categories($item, $index) {
if (array_key_exists('category', $item) {
echo $item['category'];
}
}
array_walk_recursive($categories_tree, "output_categories");
此函数循环遍历数组的每个元素并应用回调,在本例中为output_categories
。为了使输出与您需要的内容保持一致,请始终确保在输出之前有适当的条件。
您在输出中看到的实际上是不包含关联键“类别”的数组的键。当发现它正在输出第一个字母时...尝试使用上述条件,如果它仍在输出第一个字母,则只在其他位置出现错误。
答案 1 :(得分:0)
创建递归函数
未经测试的例子
function ReadCats($var)
{
foreach($var as $cat)
{
echo $cat['category'];
if(isset($cat['subcategories'])
{
$ReadCats($cats['subcategories'];
]
}
}