通过循环在php中创建多维数组
我正在尝试创建一个三维数组。 首先让我解释一下我的数据是如何来的。 我当前的数据通过循环在php中创建多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在尝试创建一个三维数组。 首先让我解释一下我的数据是如何来的。 我当前的数据 Array ( [0] => stdClass Object ([1] => A [2] => a [3] => *) [1] => stdClass Object ([1] => A [2] => a [3] => $) [2] => stdClass Object ([1] =>
Array
(
[0] => stdClass Object
([1] => A [2] => a [3] => *)
[1] => stdClass Object
([1] => A [2] => a [3] => $)
[2] => stdClass Object
([1] => B [2] => a [3] => %)
[3] => stdClass Object
([1] => B [2] => b [3] => @)
[4] => stdClass Object
([1] => B[2] => c[3] => x)
- 大写字母是我的主要标题
- 小写字母是我的副标题
- 符号是我的副标题
Array
(
[0] => A(
[0] => a(
[0] => *
[1] => $
)
)
[1] => B
(
[0] => a(
[0] => %
)
[1] => b(
[0] => $
)
[2] => c(
[0] => x
)
)
因此,首先我必须删除重复的标题,使它们成为数组,然后添加更多的数组值
到目前为止,我已经做了很多,不知道该去哪里
代码:
$query = $this->db->get();
$raw_data = $query->result();
//$array = json_decode(json_encode($data), true);
$data = array();
echo "<pre>";
foreach ($raw_data as $key => $value) {
if (in_array($value->DEPT, $data) != 1) {
$data[] = $value->DEPT;
}
}
//for here no idea what to do
foreach ($raw_data as $key => $value) {
$d_key = array_search($value->DEPT, $data[$d_key]);
if (in_array($value->CAT, $data) != 1) {
$data[$d_key] = [$value->CAT];
}
}
print_r($data);
echo "</pre>";
$query=$this->db->get();
$raw_data=$query->result();
//$array=json_decode(json_encode($data),true);
$data=array();
回声“;
foreach($key=>$value的原始数据){
如果(在数组中($value->DEPT,$data)!=1){
$data[]=$value->DEPT;
}
}
//因为我不知道该怎么办
foreach($key=>$value的原始数据){
$d_key=array_search($value->DEPT,$data[$d_key]);
如果(在数组中($value->CAT,$data)!=1){
$data[$d_key]=[$value->CAT];
}
}
打印(数据);
回声“;
我已经删除了新的重复项,我想在主标题数组中添加子标题
Array
(
[A] => Array
(
[a] => Array
(
[0] => *
[1] => $
)
)
[B] => Array
(
[a] => Array
(
[0] => %
)
[b] => Array
(
[0] => @
)
[c] => Array
(
[0] => x
)
)
)
产出:
它基本上只是为标题和副标题添加一个新的子数组(如果它不存在),然后将子标题设置为标题和副标题路径的值
Array
(
[A] => Array
(
[a] => Array
(
[0] => *
[1] => $
)
)
[B] => Array
(
[a] => Array
(
[0] => %
)
[b] => Array
(
[0] => @
)
[c] => Array
(
[0] => x
)
)
)