0

Good day everyone!

I have a CSV file, inside this:

1.453453,4.578413,0.314132,0.764312,0.123422 2.453453,3.578413,0.764312,0.314132,0.123422 3.453453,2.578413,0.123422,0.764312,0.314132 4.453453,1.578413,0.314132,0.123422,0.764312

To read this in postman this is my code:

    $keys = ['PGA', 'PGV', 'X-Axis', 'Y-Axis', 'Z-Axis'];
    $json = [];
    $path = Storage::path('upload/test.txt');
    $file = fopen($path, 'r');
    while (($line = fgetcsv($file)) !== false) {
         $json[] = array_combine($keys, $line);
    }
    fclose($file);
    return json_encode($json);

The output is :

[
    {
        "PGA": "1.453453",
        "PGV": "4.578413",
        "X-Axis": "0.314132",
        "Y-Axis": "0.764312",
        "Z-Axis": "0.123422"
    },
    {
        "PGA": "2.453453",
        "PGV": "3.578413",
        "X-Axis": "0.764312",
        "Y-Axis": "0.314132",
        "Z-Axis": "0.123422"
    },
    {
        "PGA": "3.453453",
        "PGV": "2.578413",
        "X-Axis": "0.123422",
        "Y-Axis": "0.764312",
        "Z-Axis": "0.314132"
    },
    {
        "PGA": "4.453453",
        "PGV": "1.578413",
        "X-Axis": "0.314132",
        "Y-Axis": "0.123422",
        "Z-Axis": "0.764312"
    }
]

but I need to add 'data:' before the data and be inside another {}: like this:

{    
  "data":  [
        {
            "PGA": "1.453453",
            "PGV": "4.578413",
            "X-Axis": "0.314132",
            "Y-Axis": "0.764312",
            "Z-Axis": "0.123422"
        },
        {
            "PGA": "2.453453",
            "PGV": "3.578413",
            "X-Axis": "0.764312",
            "Y-Axis": "0.314132",
            "Z-Axis": "0.123422"
        },
        {
            "PGA": "3.453453",
            "PGV": "2.578413",
            "X-Axis": "0.123422",
            "Y-Axis": "0.764312",
            "Z-Axis": "0.314132"
        },
        {
            "PGA": "4.453453",
            "PGV": "1.578413",
            "X-Axis": "0.314132",
            "Y-Axis": "0.123422",
            "Z-Axis": "0.764312"
        }
    ]
}

I am using angular as my front end and i need this to be shown. so i need to add the said above. thanks for every help you give! I have to put ' data: ' to call it inside the web page.

Thanks!

1 Answer 1

2
    $keys = ['PGA', 'PGV', 'X-Axis', 'Y-Axis', 'Z-Axis'];
    $json = [];
    $path = Storage::path('upload/test.txt');

    $file = fopen($path, 'r');
    while (($line = fgetcsv($file)) !== false) {
        $json[] = array_combine($keys, $line);
    }
    fclose($file);

    $modified = ["data" => $json];

    return json_encode($modified);
Sign up to request clarification or add additional context in comments.

1 Comment

Please consider adding an explanation or comments to the code rather than just a block of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.