I have created json in PHP from different tables. 1 json object returned is an array. How can I merge both with the second one being a sub array of the first. I want to send this as a single json to Xamarin forms (android) with a single get

PHP array merge gives Argument 2 is not an array

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
    }
]

I want the second in as an array in the first.

2 Answers

2
RiggsFolly On Best Solutions

Simply decode the 2 JSON String and then add the arrays to the class like this

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

RESULT of 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
Rakesh Kumar Jakhar On

You can use json_decode and array_merge to approach this

$json1ToArray = json_decode($json1, true);
$json2ToArray = json_decode($json2, true);
$res = array_merge($json1ToArray, $json2ToArray);
print_r($res); 

Live Demo