我有以下数据来自循环:
$array = Array(
[0]=>Array(
[0]=>Cat 1
[1]=> Q1
[2]=> A1
)
[1]=>Array(
[0]=>Cat 1
[1]=> Q2
[2]=> A2
)
[2]=>Array(
[0]=>Cat 2
[1]=> Q1
[2]=> A1
)
[3]=>Array(
[0]=>Cat 3
[1]=>Sub Cat 3
[2]=>Q1
[3]=>A1
)
[4]=>Array(
[0]=>Cat 3
[1]=>Sub Cat 3
[2]=>Q2
[3]=>A2
)
)
)
我需要数组中的数据,如
$array_res = Array(
['Cat 1']=>Array(
[0]=>Array(
[0]=> Q1
[1]=> A1
)
[1]=>Array(
[0]=> Q2
[1]=> A2
)
)
['Cat 2']=>Array(
[0]=>Array(
[0]=> Q1
[1]=> A1
)
)
['Cat 3']=>Array(
['Sub Cat 3']=>Array(
[0]=>Array(
[0]=> Q1
[1]=> A1
)
[1]=>Array(
[0]=> Q2
[1]=> A2
)
)
)
)
它应该返回Array / Associative Array中的数据。因此可以循环获取数据&打印出来。我希望在数组内有数组值时动态创建数组,如子cat 3。
答案 0 :(得分:0)
这是代码。我用我的评论解释了我的回答。你可以在这里试试https://3v4l.org/K1kZ6
<强>答案:强>
$cat_array = array(
0=>array(
0=>'Cat 1',
1=>'Q1',
2=>'A1'
),
1=>array(
0=>'Cat 1',
1=>'Q2',
2=>'A2'
),
2=>array(
0=>'Cat 2',
1=>'Q1',
2=>'A1'
),
3=>array(
0=>'Cat 3',
1=>'Sub Cat 3',
2=>'Q1',
3=>'A1'
),
4=>array(
0=>'Cat 3',
1=>'Sub Cat 3',
2=>'Q2',
3=>'A2'
)
);
$converted_array = array();
foreach($cat_array as $key => $array){
// Split the cat and 3
$temp = explode(' ', $array[0]);
//Check if key(Cat 1,2,3,etc..) exists in converted array
if(!array_key_exists($array[0],$converted_array)){
$converted_array[$array[0]] = array();
}
//Get and check if there is a Sub Cat
$sub_temp = explode(' ', $array[1]);
//Check in array if there is a sub in $sub_temp array.
if(!in_array('sub' , array_map("strtolower", $sub_temp))){
//If doesn't have sub
//Store key(Cat 1,2,3...) before we unset
$temp_key = $array[0];
unset($array[0]);
//Get the total count to provide as our starting index
$l = count($converted_array[$temp_key]);
// $temp_key = "Cat 1,2,3,etc..." and $l = 0,1,2...
foreach($array as $val){
$converted_array[$temp_key][$l][]= $val;
}
}else{
//Check if sub is inside of sub array
if(!array_key_exists($array[1],$converted_array[$array[0]])){
$converted_array[$array[0]][$array[1]] = array();
}
//Same as the above, we store key first before we unset.
$temp_key = $array[0];
$temp_sub_key = $array[1];
unset($array[0]);
unset($array[1]);
//Same as the above but with sub category
$l = count($converted_array[$temp_key][$temp_sub_key]);
foreach($array as $val){
$converted_array[$temp_key][$temp_sub_key][$l][]= $val;
}
}
}
echo "<pre>";
print_r($converted_array);
<强>输出:强>
Array
(
[Cat 1] => Array
(
[0] => Array
(
[0] => Q1
[1] => A1
)
[1] => Array
(
[0] => Q2
[1] => A2
)
)
[Cat 2] => Array
(
[0] => Array
(
[0] => Q1
[1] => A1
)
)
[Cat 3] => Array
(
[Sub Cat 3] => Array
(
[0] => Array
(
[0] => Q1
[1] => A1
)
[1] => Array
(
[0] => Q2
[1] => A2
)
)
)
)
答案 1 :(得分:0)
没什么可解释的。也照顾了子类别。
<?php
$array = Array(
Array(
"Cat 1",
"Q1",
"A1"
),
Array(
"Cat 1",
"Q2",
"A2"
),
Array(
"Cat 2",
"Q1",
"A1"
),
Array(
"Cat 3",
"Sub Cat 3",
"Q1",
"A1"
),
Array(
"Cat 3",
"Sub Cat 3",
"Q2",
"A2"
),
Array(
"Cat 2",
"Q2",
"A2"
)
);
$array_res = array();
foreach($array as $key=> $val) {
if(is_array($val)) {
$catName = $val[0];
//echo "category = $catName <br>";
//echo count($val) . "<br>";
if(count($val) === 3) {
// no sub category
$this_arr = array();
foreach($val as $key1 => $val1) {
if($key1 !=0) {
//echo "$key1 => $val1 <br>";
$this_arr[] = $val1;
}
}
$array_res[$catName][] = $this_arr;
}
else {
// there is subcategory as well
$subCatName = $val[1];
$this_arr = array();
foreach($val as $key1 => $val1) {
if($key1 !=0 && $key1 !=1 ) {
//echo "$key1 => $val1 <br>";
$this_arr[] = $val1;
}
}
$array_res[$catName][$subCatName][] = $this_arr;
}
}
else {
// echo "$key => $val <br/>";
}
}
echo "<br/>Result Array <br>";
foreach($array_res as $key => $val) {
if(is_array($val)) {
echo "$key<br>";
foreach($val as $key1 => $val1) {
echo "$key1 <br>";
if(is_array($val1)) {
foreach($val1 as $key2 => $val2) {
if(is_array($val2)) {
//echo "$key2 <br>";
foreach($val2 as $key3 => $val3) {
echo "$key3 => $val3 <br>";
}
}
else {
echo "$key2 => $val2 <br>";
}
}
}
}
}
else {
echo "$key => $val <br/>";
}
}
?>