我用PHP在不同的表中创建了json。返回的1个json对象是一个数组。如何将第二个都合并为第一个的子数组。我想通过单个get将其作为单个json发送到Xamarin表单(android)
PHP数组合并给出参数2不是数组
Json1
{
"employeeid": "1123",
"employeename": "EMP 001 NAME",
"mMacID": "E0138",
"machinename": "FOS",
"iscleaning": 1,
"isperforming": 1,
"isverifying": 1,
"cSeqno": 1,
"cMacID": "E0138",
"cInterval": 112,
"cCleanOperationMaxTime": 300,
"cPerformOperationMaxTime": 600,
"oSequenceID": 6,
"oMacID": "E0138",
"oItemNumber": " ",
"oBatchNumber": " ",
"oPONumber": " ",
"oCompletedOperation": 0,
"oComplOperStartTime": 0,
"oCompOperEndndTime": 0,
"oOperationToContinue": 1
}
Json2 (Array)
[
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 1,
"pLocationName": "TestLoc1",
"pLocationInterval": 12,
"pImageRequiredForVerifying": 1,
"pErrorFound": 0
},
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 2,
"pLocationName": "TestLoc2",
"pLocationInterval": 15,
"pImageRequiredForVerifying": 0,
"pErrorFound": 0
},
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 3,
"pLocationName": "TESTLOC3",
"pLocationInterval": 18,
"pImageRequiredForVerifying": 0,
"pErrorFound": 0
},
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 1,
"pLocationName": "LOC1",
"pLocationInterval": 12,
"pImageRequiredForVerifying": 0,
"pErrorFound": 0
}
]
我想要第二个作为第一个数组。
答案 0 :(得分:2)
只需解码2个JSON字符串,然后将数组添加到此类中
$js1 = '{
"employeeid": "1123",
"employeename": "EMP 001 NAME",
"mMacID": "E0138",
"machinename": "FOS",
"iscleaning": 1,
"isperforming": 1,
"isverifying": 1,
"cSeqno": 1,
"cMacID": "E0138",
"cInterval": 112,
"cCleanOperationMaxTime": 300,
"cPerformOperationMaxTime": 600,
"oSequenceID": 6,
"oMacID": "E0138",
"oItemNumber": " ",
"oBatchNumber": " ",
"oPONumber": " ",
"oCompletedOperation": 0,
"oComplOperStartTime": 0,
"oCompOperEndndTime": 0,
"oOperationToContinue": 1
}';
$js2 = '[
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 1,
"pLocationName": "TestLoc1",
"pLocationInterval": 12,
"pImageRequiredForVerifying": 1,
"pErrorFound": 0
},
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 2,
"pLocationName": "TestLoc2",
"pLocationInterval": 15,
"pImageRequiredForVerifying": 0,
"pErrorFound": 0
},
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 3,
"pLocationName": "TESTLOC3",
"pLocationInterval": 18,
"pImageRequiredForVerifying": 0,
"pErrorFound": 0
},
{
"pMachineID": "E0138",
"pmachinetoLocationSequence": 1,
"pLocationNumber": 1,
"pLocationName": "LOC1",
"pLocationInterval": 12,
"pImageRequiredForVerifying": 0,
"pErrorFound": 0
}
]';
$j1 = json_decode($js1);
$j2 = json_decode($js2);
// You may want to give this a more sensible name than `theArrays`
$j1->theArrays = $j2;
print_r($j1);
$new_json_string = json_encode($j1);
echo $new_json_string;
print_r($j1)
的结果
stdClass Object
(
[employeeid] => 1123
[employeename] => EMP 001 NAME
[mMacID] => E0138
[machinename] => FOS
[iscleaning] => 1
[isperforming] => 1
[isverifying] => 1
[cSeqno] => 1
[cMacID] => E0138
[cInterval] => 112
[cCleanOperationMaxTime] => 300
[cPerformOperationMaxTime] => 600
[oSequenceID] => 6
[oMacID] => E0138
[oItemNumber] =>
[oBatchNumber] =>
[oPONumber] =>
[oCompletedOperation] => 0
[oComplOperStartTime] => 0
[oCompOperEndndTime] => 0
[oOperationToContinue] => 1
[theArrays] => Array
(
[0] => stdClass Object
(
[pMachineID] => E0138
[pmachinetoLocationSequence] => 1
[pLocationNumber] => 1
[pLocationName] => TestLoc1
[pLocationInterval] => 12
[pImageRequiredForVerifying] => 1
[pErrorFound] => 0
)
[1] => stdClass Object
(
[pMachineID] => E0138
[pmachinetoLocationSequence] => 1
[pLocationNumber] => 2
[pLocationName] => TestLoc2
[pLocationInterval] => 15
[pImageRequiredForVerifying] => 0
[pErrorFound] => 0
)
[2] => stdClass Object
(
[pMachineID] => E0138
[pmachinetoLocationSequence] => 1
[pLocationNumber] => 3
[pLocationName] => TESTLOC3
[pLocationInterval] => 18
[pImageRequiredForVerifying] => 0
[pErrorFound] => 0
)
[3] => stdClass Object
(
[pMachineID] => E0138
[pmachinetoLocationSequence] => 1
[pLocationNumber] => 1
[pLocationName] => LOC1
[pLocationInterval] => 12
[pImageRequiredForVerifying] => 0
[pErrorFound] => 0
)
)
)
答案 1 :(得分:1)
您可以使用json_decode
和array_merge
来解决这个问题
$json1ToArray = json_decode($json1, true);
$json2ToArray = json_decode($json2, true);
$res = array_merge($json1ToArray, $json2ToArray);
print_r($res);