输入str =“102_254,102_274,102_232,104_225,104_226,103_233,103_229,101_316,101_244,101_249,101_250”
此处输入为“102_254”表示“Menuid_Submenuid”
表名是:
表格字段为:
我想从“SubMenuTable”表中选择项目名称并将其响应为JSON格式
输出将像这样分开:
我想在JSON Formate中输出:
{ “0”:{ “101”:{ “0”:{ “MENUID”: “101”, “submenuid”: “316”, “项目”: “ItemName1”}, “1”:{” MENUID “:” 101" , “submenuid”: “244”, “项目”: “ItemName2”}, “2”:{ “MENUID”: “101”, “submenuid”: “249”, “项目”:” ItemName3 “},” 3 “:{” MENUID “:” 101" , “submenuid”: “250”, “项目”: “ItemName4”}}, “102”:{ “0”:{ “MENUID”:” 102" , “submenuid”: “254”, “项目”: “ItemName1”}, “1”:{ “MENUID”: “102”, “submenuid”: “274”, “项目”: “ItemName2”}, “2”:{ “MENUID”: “102”, “submenuid”: “232”, “项目”: “ItemName3”}}, “103”:{ “0”:{ “MENUID”: “103”,” submenuid “:” 233" , “项目”: “ItemName1”}, “1”:{ “MENUID”: “103”, “submenuid”: “229”, “项目”: “ItemName2”}}, “104” :{ “0”:{ “MENUID”: “104”, “submenuid”: “225”, “项目”: “ItemName1”}, “1”:{ “MENUID”: “104”, “submenuid”:” 226" , “项目”: “ItemName2”}}}}
答案 0 :(得分:1)
这是一个重新格式化的原型,只需要预先添加项目并对每个子数组进行编码:
$sInput = '102_254,102_274,102_232,104_225,104_226,103_233,103_229,101_316,101_244,101_249,101_250';
$aInput = explode( ',', $sInput );
$iCountInputs = count( $aInput );
for( $i = 0; $i < $iCountInputs; ++$i )
{
$aInner = explode( '_', $aInput[ $i ] );
$aTmp[ $aInner[ 0 ] ][] = $aInner[ 1 ];
}
$sJson = json_encode( $aTmp );
var_dump( $aInput );
var_dump( $sJson );
$aAfter = json_decode( $sJson, 1 );
var_dump( $aAfter );