PHP And JSON

To explain the relationship between PHP and Json and how to use Json in PHP, let’s start with “What’s Json” on top of it.

In programming, there is a need for data exchange between different platforms. For example, you might want to pull out the latest posts from any social platform and use them in your own project. There is a need to share between platforms for such transactions, and some technologies have been developed for this. For example, one of them is XML technology, but XML is not well used with Javascript. Json is preferred because it is compatible with Javascript.

The data in Json is in the form of array and object.

Object

It starts with “{“ and ends with “}”. The data is written in “Key” and “Data”. Sample;

{
    "name": "Erhan",
    "surname": "Kılıç"
}

Array

It starts with “[“ and ends with “]”. The data in it is written only as “Data”. Keys are automatically identified. Sample;

[
    "Erhan", "Kılıç"
]

Both can be used together in Json as desired. Sample;

{
    "type": "book",
    "genre": "novel",
    "products": [
        {"name": "Ihtiyar Kemanci", "author": "Nihat Genc"},
        {"name": "Su Cilgin Turkler", "author": "Turgut Ozakman"},
        {"name": "Kar", "author": "Orhan Pamuk"}
    ]
}

The most important reason why Json is preferred is that the object and array structure in Javascript are exactly the same. As an example, you will also notice when we try to do it in Javascript.

Json Functions in PHP

In PHP, Json-related operations can be used for the following reason; to use it in Javascript or libraries like jQuery, AngularJs on our own site, to share data with other platforms and to create api. We can use the data that we extracted with Json from anywhere in our php application.

If we pull the json data from elsewhere, we need to be careful; if we do not use the following functions, php will treat the json data as a string value. In fact, everywhere perceives json data as a string, unless the necessary markers are used.

json_encode();

This function allows you to print the data as Json. Sample Usage;

<?php
$array = [
    "type"=> "book",
    "genre"=> "novel",
    "products"=> [
        ["name"=> "Ihtiyar Kemanci", "author"=> "Nihat Genc"],
        ["name"=> "Su Cilgin Turkler", "author"=> "Turgut Ozakman"],
        ["name"=> "Kar", "author"=> "Orhan Pamuk"]
    ]
];
$json = json_encode($array);
echo $json;

json_decode();

This function allows us to decode the Json data we extract from a place and use it in our php software. We can access the data in Json as accessing the data on object. In the following example, I’ll create json data as string. Because when we extract Json data from outside, we will get string data in the same way. Sample;

<?php
$json = '{
    "type": "book",
    "genre": "novel",
    "products": [
        {"name": "Ihtiyar Kemanci", "author": "Nihat Genc"},
        {"name": "Su Cilgin Turkler", "author": "Turgut Ozakman"},
        {"name": "Kar", "author": "Orhan Pamuk"}
    ]
}';
$data = json_decode($json);
echo $data->type;
echo $data->genre;
foreach($data->products as $product){
    var_dump($product);
    echo "<br/>";
}

If you add true as a second parameter in the function, the data will be stored as an array rather than as an object, and our access will change accordingly. Sample;

<?php
$json = '{
    "type": "book",
    "genre": "novel",
    "products": [
        {"name": "Ihtiyar Kemanci", "author": "Nihat Genc"},
        {"name": "Su Cilgin Turkler", "author": "Turgut Ozakman"},
        {"name": "Kar", "author": "Orhan Pamuk"}
    ]
}';
$data = json_decode($json, true);
echo $data["type"];
echo $data["genre"];
foreach($data["products"] as $product){
    var_dump($product);
    echo "<br/>";
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments