How to Create a JSON file using PHP

How to Create a JSON file using PHP

In this article we are going to see how easy it is to create a JSON file using PHP.

In the example that will illustrate let’s create a JSON file in which we will deposit product information, specifically on modules for prestashop.

We have dynamically created an array (which could be collected from a database for example) with information on products such as: product id, name and price, obtaining a multidimensional array like this:

Array
(
    [0] => Array
        (
            [Id_product] => 89
            [Name] => Best Price Guaranteed
            [Price] => 40
        )

    [1] => Array
        (
            [Id_product] => 68
            [Name] => CSV Specific Prices
            [Price] => 30
        )

    [2] => Array
        (
            [Id_product] => 59
            [Name] => Block Search Suggestions
            [Price] => 30
        )

    [3] => Array
        (
            [Id_product] => 94
            [Name] => Management downloadable products
            [Price] => 40
        )

)

Now we want to turn this array into a JSON file using the json_encode () function.

$ Data_to_file_json = json_encode ($ products);

Once you have the file contents and just missing create a file and place this content.

Lucid Kit

Related Posts
Leave a reply
Captcha Click on image to update the captcha .