php - PHP - 将 Json 数组结构分组到多维数组和 json_decode 不起作用
问题描述
有人可以借给我一些帮助吗……以下是我对它们进行分组和分组的新数据
{
"id":"1",
"ppmp_id":"43",
"allot_class":"Expenditures",
"fund_code":"50202010",
"fund_desc":"50202010 Training ",
"qty":"2",
"unit_cost":"100000.00",
"total_cost":"200000.00",
"qty_jan":"2",
"qty_feb":"1",
"qty_mar":"1",
"qty_apr":"1",
"description":"Isoprophyl Alcohol Gardian "
},
{
"id":"2",
"ppmp_id":"43",
"allot_class":"Expenditures",
"fund_code":"50202010",
"fund_desc":"50202010 Training ",
"qty":"5","unit_cost":"50000.00",
"total_cost":"100000.00",
"qty_jan":"1",
"qty_feb":"1",
"qty_mar":"1",
"qty_apr":"2",
"description":"Rebisco Wafer Time Rich Cream"
},
{
"id":"5",
"ppmp_id":"43",
"allot_class":"Maintenance,
"fund_code":"45354354",
"fund_desc":"Dev","qty":"1",
"unit_cost":"45000.00",
"total_cost":"45000.00",
"qty_jan":"1",
"qty_feb":"2",
"qty_mar":"3",
"qty_apr":"4",
"description":"Radeon Vega GraphicsSocket"
},
{
"id":"6",
"ppmp_id":"43",
"allot_class":"Maintenance",
"fund_code":"45354354",
"fund_desc":"Dev",
"qty":"2","unit_cost":"10000.00",
"total_cost":"20000.00",
"qty_jan":"1",
"qty_feb":"2",
"qty_mar":"4",
"description":"Graphics EngineNVIDIA GeForce GTX 1050"
},
{
"id":"8",
"ppmp_id":"43",
"allot_class":"Other Optg.",
"fund_code":"78677899",
"fund_desc":"Supplies",
"qty":"5","unit_cost":"12000.00",
"total_cost":"60000.00",
"qty_jan":"2",
"qty_feb":"5",
"qty_mar":"6",
"qty_apr":"2",
"description":"Core MemoryBoost Clock"
},
{
"id":"9",
"ppmp_id":"43",
"allot_class":"Other Optg.",
"fund_code":"78677899",
"fund_desc":"Supplies",
"qty":"1","unit_cost":"74998.00",
"total_cost":"74998.00",
"qty_jan":"3",
"qty_feb":"5",
"qty_mar":"5",
"qty_apr":"1",
"description":"Core USB Type"
},
我发现很难按这种格式对它们进行分组和分组
首先是按 allot_class 分组,然后按基金代码分组,然后再按描述分组,描述为每月数量
分配类
- fund_code
- description
-qty_jan
-qty_feb
-qty_mar
-qty-apr
分配类
- fund_code
- description
-qty_jan
-qty_feb
-qty_mar
-qty-apr
我试过这段代码,但它说
消息:json_decode() 期望参数 1 是字符串,给定数组
我也在沙箱中试过它工作正常 $command = "CALL sp_ppmp_items_get_all_per_ppmp(?);";
$proc_db = $this->load->database('pgbhus_procurement', TRUE);
$result = $proc_db->query($command, $data);
$proc_db->close();
if ($result) {
$data_arr = $result->result_array();
//the result here was as stated above
if (count($data_arr) > 0) {
$new_array = json_decode($data_arr, true);
$testarr = [];
foreach ($new_array as $key1=>$val1){
foreach ($val1 as $key2=>$val2){
if($key2=='allot_class'){
$num=$val2;
if($val2!=$num)
$testarr['allot_class '.$val2]=array();}
if($key2=='fund_desc'){
$testarr['allot_class '.$num]['fund_desc '.$val2][]=$val1;} }}
return $testarr;
} else {
return "No data found"; } }
return "Error has occured"; }
我想完成这个,这是我在沙箱中执行的输出
结果
Array
(
[allot_class Expenditures] => Array
(
[fund_desc 50202010 Training] => Array
(
[0] => Array
(
[id] => 1
[ppmp_id] => 43
[allot_class] => Expenditures
[fund_code] => 50202010
[fund_desc] => 50202010 Training
[qty] => 2
[unit_cost] => 100000.00
[total_cost] => 200000.00
[qty_jan] => 2
[qty_feb] => 1
[qty_mar] => 1
[qty_apr] => 1
[description] => Isoprophyl Alcohol Gardian
)
)
[fund_desc 50202010 Training ] => Array
(
[0] => Array
(
[id] => 2
[ppmp_id] => 43
[allot_class] => Expenditures
[fund_code] => 50202010
[fund_desc] => 50202010 Training
[qty] => 5
[unit_cost] => 50000.00
[total_cost] => 100000.00
[qty_jan] => 1
[qty_feb] => 1
[qty_mar] => 1
[qty_apr] => 2
[description] => Rebisco Wafer Time Rich Cream
)
)
)
[allot_class Maintenance] => Array
(
[fund_desc Dev] => Array
(
[0] => Array
(
[id] => 5
[ppmp_id] => 43
[allot_class] => Maintenance
[fund_code] => 45354354
[fund_desc] => Dev
[qty] => 1
[unit_cost] => 45000.00
[total_cost] => 45000.00
[qty_jan] => 1
[qty_feb] => 2
[qty_mar] => 3
[qty_apr] => 4
[description] => Radeon Vega GraphicsSocket
)
[1] => Array
(
[id] => 6
[ppmp_id] => 43
[allot_class] => Maintenance
[fund_code] => 45354354
[fund_desc] => Dev
[qty] => 2
[unit_cost] => 10000.00
[total_cost] => 20000.00
[qty_jan] => 1
[qty_feb] => 2
[qty_mar] => 4
[description] => Graphics EngineNVIDIA GeForce GTX 1050
)
)
)
[allot_class Other Optg.] => Array
(
[fund_desc Supplies] => Array
(
[0] => Array
(
[id] => 8
[ppmp_id] => 43
[allot_class] => Other Optg.
[fund_code] => 78677899
[fund_desc] => Supplies
[qty] => 5
[unit_cost] => 12000.00
[total_cost] => 60000.00
[qty_jan] => 2
[qty_feb] => 5
[qty_mar] => 6
[qty_apr] => 2
[description] => Core MemoryBoost Clock
)
[1] => Array
(
[id] => 9
[ppmp_id] => 43
[allot_class] => Other Optg.
[fund_code] => 78677899
[fund_desc] => Supplies
[qty] => 1
[unit_cost] => 74998.00
[total_cost] => 74998.00
[qty_jan] => 3
[qty_feb] => 5
[qty_mar] => 5
[qty_apr] => 1
[description] => Core USB Type
)
)
)
)
解决方案
似乎 $data_arr 是一个数组,根据json_decode() 的文档,第一个参数必须是一个字符串。我不知道这个数组的类型是什么,但代码必须是这样的
$new_array = json_decode($data_arr["index"], true);
推荐阅读
- mysql - MySQL INSERT 使用 COUNT() 和 GROUP BY 错误`无效使用组函数`
- arrays - 如何访问闭包之外的数组?
- python - 每当使用 import cv2 时,OpenCV 都会出错
- scala - 如何在 spark scala 中删除换行符
- r - Flexdashboard checkboxGroupInput丢失ggplot数据
- java - setDisplayHomeAsUpEnabled 在我的一项活动中不起作用,但在其他任何地方都起作用
- jquery - jquery if ($("#notexist").length > 0) 抛出错误
- json - 如何从 URL 解析 Json 并在 Xcode 的 Collection View 中显示
- php - 无法访问 std 类对象上的 php stdClass 属性
- intellij-idea - 编译latex文件后如何使Pycharm自动查看pdf?