繁体   English   中英

PHP - 将 Json 数组结构分组到多维数组和 json_decode 不起作用

[英]PHP -grouping Json array structure to multidimensional array and json_decode not working

有人可以借给我一些帮助吗……以下是我对它们进行分组和分组的新数据

{

    "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"; }

我想做到这一点,这是我在沙箱中执行的 output

结果

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM