Ângelo Rigo
Ângelo Rigo

Reputation: 2157

Generate the set of data into Json instead of one big mixed Json

How can i create one Json with many small sets of data separated by comma?

Instead of one big Json enclosed by double curly brackets?

I do receive a Json and with php i do use foreach to loop over it, making a lot of data processing inside.

Then generate a new Json, just to avoid the data processing on the client side wich will be processed by angularjs ng-repeat.

All the json data is mixed into one big json set (inside double curly brackets)

My goal is to separate into small sets of data.

I can use the NrType property. In this script the NrType receives the last atribution and just the last received is available.

//The php script

    $arr = json_decode($returnedJson); //The original json to be pre-processed
    $processedData = "[]";
    $processedJson = json_decode($processedData,true);

    foreach($arr as $key=>$value) { 
        foreach($value as $vkey=>$vvalue) {             

            if( $value[$i]->NrType == 1 ) {

                $VlMIni     = $value[$i]->QttyInitial;
                $VlMSub     = $value[$i]->QttyPeriod + $value[$i]->QttyRealAfter;
                $VlMRec     = $value[$i]->RealValue;
                $VlMTotal   += $VlMesRece;
                //much more data processing going on here ...

            } elseif( $value[$i]->NrType == 2 ) { 

            .
            .
            .
            //and much more data processing going on here ...
        }
    }

    //simple data atribution here
    $processedJson['labelDesIni']       = 'Instruments';
    $processedJson['labelValueMIni']    = $lblVlMIni;   
    $processedJson['labelValuePIni']    = $lblVlPlIni; 
    $processedJson['labelValueAIni']    = $lblVlAIni;   
    $processedJson['labelValuePAIni']   = $lblVlPAIni;  
    $processedJson['labelValuePercInic']    = $lblVlPercInic;   
    $processedJson['labelValuePerc2Inic']   = $lblVlPerc2Inic; 

    //much more data atribution ...

    echo json_encode($processedJson); //the new hgenerated Json

The generated Json :

{
    labelDesI: "Inspection",
    labelValueMI: "2357",
    labelValuePlI: "3914066",
    labelValueAI: "1389406",
    labelValuePAI: "2431425",
    labelValuePercI: 57.143691456656,
    labelValuePerc2I: 35.497766261478,
    labelDesR: "Instruments",
    labelValueMR: "734.54",
    labelValuePR: "819.14",
    labelValueAR: "660.05",
    labelValuePAR: "877.94",
    labelValuePercR: 80.087,
    labelValuePerc2R: 44.739,
    labelDesAcfi: "Fiscalização",
    labelValueMAcfi: "343",
    labelValuePlAcfi: "29907",
    labelValueAAcfi: "16718",
    labelValuePAAcfi: "16493",
    labelValuePercAcfi: 101.36421512157,
    labelValuePerc2Acfi: 55.899956531916,
    labelDesT: "Totals",
    labelValueMT: 365.59,
    labelValuePlT: 547.62,
    labelValueAnT: 909.63,
    labelValuePAnT: 957.63,
    labelValuePercT: 22949,
    labelValuePerc2T: 25065
}

The desired format Would be this :

{
    label: "Inspection",
    labelValue1: "2357",
    labelValue2: "3914066",
    labelValue3: "1389406",
    labelValue4: "2431425",
    labelValue5: 57.1456656,
    labelValue6: 35.4961478
},
{
    labelDesR: "Instruments",
    labelValue1: "734.54",
    labelValue2: "819.14",
    labelValue3: "660.05",
    labelValue4: "877.94",
    labelValue5: 80.087,
    labelValue6: 44.739
},
{
    labelDesT: "Totals",
    labelValue1: 365.59,
    labelValue2: 547.62,
    labelValue3: 909.63,
    labelValue4: 957.63,
    labelValue5: 22949,
    labelValue6: 25065
}

Thank´s in advance

Upvotes: 0

Views: 51

Answers (1)

eggmatters
eggmatters

Reputation: 1140

generate all of your object separately, create an array of these objects and json_encode the array:

$processedJsonElement[] = ['labelDesT' => "Totals", 'labelValue1' => $whatTheValueIs, . . .];

and add it to you main object:

$processedJson[] = $processedJsonElement;

do this for each section of Json you want to represent. Not sure how you're structuring your Foreach loop as your code doesn't match the output, but whatever you structure is whatever you will will output when you call json_encode.

Basically, you need to structure your foreach loop to be able to compartmentalize the objects you wish to represent as an array of json objects.

Upvotes: 1

Related Questions