我表格中的一栏有
之类的数据category1_subcat_1
category1_subcat_2
category1_subcat_3
category2_subcat_1
category2_subcat_2
category3_subcat_1
category3_subcat_3
我想将其转换为嵌套数组,如
category1
subcat1
subcat2
subcat3
category2
subcat1
subcat2
category3
subcat1
subcat3
我试过
foreach ($categories as $category){
$cat_array = explode("_",$category['cat_subcat']);
$cat_subcat = array(
'cat'=>$cat_array[0],
'subcat'=>$cat_array[1]
);
}
但如何按类别对此进行分组请帮助
答案 0 :(得分:1)
试试这个..
<?php
$categories = [
['cat_subcat' => 'category1_subcat_1'],
['cat_subcat' => 'category1_subcat_2'],
['cat_subcat' => 'category1_subcat_3'],
['cat_subcat' => 'category2_subcat_1'],
['cat_subcat' => 'category2_subcat_2'],
['cat_subcat' => 'category3_subcat_1'],
['cat_subcat' => 'category3_subcat_3']
];
$finalCategories = [];
foreach ($categories as $category){
$cat_array = explode("_",$category['cat_subcat']);
$finalCategories[$cat_array[0]][] = $cat_array[1] . $cat_array[2];
}
echo "<pre>";
print_r($finalCategories);
输出将是......
Array
(
[category1] => Array
(
[0] => subcat1
[1] => subcat2
[2] => subcat3
)
[category2] => Array
(
[0] => subcat1
[1] => subcat2
)
[category3] => Array
(
[0] => subcat1
[1] => subcat3
)
)
添加以下代码行..
// Adding another loop
$output = [];
$i = 0;
foreach ($finalCategories as $category => $subCategories) {
$output[$i]['cat_name'] = $category;
foreach ($subCategories as $subCategory) {
$output[$i]['sub_cat'][] = $subCategory;
}
$i++;
}
echo "<pre>";
print_r($output);
New Ouptut ..
Array
(
[0] => Array
(
[cat_name] => category1
[sub_cat] => Array
(
[0] => subcat1
[1] => subcat2
[2] => subcat3
)
)
[1] => Array
(
[cat_name] => category2
[sub_cat] => Array
(
[0] => subcat1
[1] => subcat2
)
)
[2] => Array
(
[cat_name] => category3
[sub_cat] => Array
(
[0] => subcat1
[1] => subcat3
)
)
)
答案 1 :(得分:0)
<?php
$str = <<<STR
category1_subcat_1
category1_subcat_2
category1_subcat_3
category2_subcat_1
category2_subcat_2
category3_subcat_1
category3_subcat_3
STR;
// Explode the string, with a newline as the delimiter
$lines = explode("\n", $str);
// Init the category array
$cats = [];
// iterate over the lines
foreach($lines as $currentLine) {
// Match category{DIGIT} using regular exp
preg_match("/category[0-9]/", $currentLine, $match);
// Remove the $match from the current line
$currentLine = str_replace($match[0], "", $currentLine);
// Push the element to the categories array, and also remove the
// the underscores (_)
$cats[$match[0]][] = str_replace("_", "", $currentLine);
}
var_dump($cats);
array(3) {
["category1"]=>
array(3) {
[0]=>
string(7) "subcat1"
[1]=>
string(7) "subcat2"
[2]=>
string(7) "subcat3"
}
["category2"]=>
array(2) {
[0]=>
string(7) "subcat1"
[1]=>
string(7) "subcat2"
}
["category3"]=>
array(2) {
[0]=>
string(7) "subcat1"
[1]=>
string(7) "subcat3"
}
}
答案 2 :(得分:0)
因为OP要求嵌套数组而不是字典:
<?php
$nested_array = [];
foreach ($categories as $category){
$exploded = explode("_",$category);
if ($nested_array[-1,1] != $exploded[0]) {
array_push($nested_array, $exploded[0])
}
array_push($nested_array[-1,1], $exploded[1].$exploded[2])
}
?>
缺少的部分实际上只是一个控制流程,用于在使用foreach
遍历数组时检查是否已达到新类别。因此结果存储在$nested_array
。
if ($nested_array[-1,1] != $exploded[0])
正在检查$nested_array
中存储的最后一个类别(最后一个元素)是否等于字符串的第一部分,如:"category1" != "category1"
或"category1" != "category2"
等。