PHP and JSON

phpjsonPHP And Json the relationship between the two and Json'reputation PHPTo explain its use in , first of all Json Let's focus on what it is.

In programming, there's a need to exchange data between different platforms. For example, you might want to pull the latest posts from a social platform and use them in your own project. For such operations, there needs to be sharing between platforms. Certain technologies have been developed for this sharing. For example, one of these is XML technology. But XML, Javascript It cannot be used well with. Here it is Json, Javascript It is preferred because it is compatible with.

Json the data contained in each programming soap opera And object It is in the form of.

Object

“{“ starts with and “}” It ends with "Data inside"Key" And "DataIt is written as ”. Example;

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

Soap opera

“[“ starts with and “]” It ends with . The data contained therein is only "DataIt is written as ”. Example;

[ "Erhan", "Sword" ]

Json Both can be used together as desired. For example;

{ "type": "book", "genre": "novel", "products": [ {"name": "The Old Violinist", "author": "Nihat Genc"}, {"name": "Su Crazy Turkler", "author": "Turgut Ozakman"}, {"name": "Snow", "author": "Orhan Pamuk"} ] }

JsonThe most important reason for choosing JavascriptThe object and array structure in is exactly the same. For example, JavascriptYou will notice when we try to do the same in .

Json Functions in PHP

PHP'also Json Transactions related to can be done as follows. On our own website Javascript or jQuery, AngularJs to use in libraries such as, to share data with other platforms and to create APIs Json We can print it to the screen in format. From another location Json We can use the data we retrieved in our PHP application.

One thing we need to be careful about is this: If we don't use the functions below, it will be interpreted as a string value, making it quite difficult to access the data inside.

json_encode()

This function returns the data we want Json It allows us to print as Example Usage;

$dizi = array( "type"=> "book", "type"=> "novel", "products"=> array( array("name"=> "Old Violinist", "author"=> "Nihat Genc"), array("name"=> "Su Crazy Turkler", "author"=> "Turgut Ozakman"), array("name"=> "Snow", "author"=> "Orhan Pamuk") ) ); $json = json_encode($dizi); echo $json;

json_decode()

This function is something we pull from somewhere. Json It decodes the data and allows us to use it within our PHP software. We can access the data inside as if we were accessing data in an object. In the example below: Json I create the data as a string data. Because from outside Json When we pull data, it will come as a string data in the same way. For example;

$json =&#039;{ &quot;type&quot;: &quot;book&quot;, &quot;genre&quot;: &quot;novel&quot;, &quot;products&quot;: [ {&quot;name&quot;: &quot;Ihyar Kemanci&quot;, &quot;author&quot;: &quot;Nihat Genc&quot;}, {&quot;name&quot;: &quot;Su Çilgin Turkler&quot;, &quot;author&quot;: &quot;Turgut Ozakman&quot;}, {&quot;name&quot;: &quot;Kar&quot;, &quot;author&quot;: &quot;Orhan Pamuk&quot;} ] }&#039;; $veri = json_decode($json); echo $veri-&gt;type; echo $veri-&gt;genre; foreach($veri-&gt;products as $urun){ echo vardumb($urun); echo &quot;<br/>";
}

If we add true as a second parameter in the function object not as a direct soap opera and our access will change accordingly. For example;

$json =&#039;{ &quot;type&quot;: &quot;book&quot;, &quot;genre&quot;: &quot;novel&quot;, &quot;products&quot;: [ {&quot;name&quot;: &quot;Ihyar Kemanci&quot;, &quot;author&quot;: &quot;Nihat Genc&quot;}, {&quot;name&quot;: &quot;Su Çilgin Turkler&quot;, &quot;author&quot;: &quot;Turgut Ozakman&quot;}, {&quot;name&quot;: &quot;Kar&quot;, &quot;author&quot;: &quot;Orhan Pamuk&quot;} ] }&#039;; $veri = json_decode($json, true); echo $veri[type]; echo $veri[gen]; foreach($veri[products] as $urun){ echo vardumb($urun); echo &quot;<br/>";
}
2 1 vote
Article Rating
Subscribe
Notify of
guest

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

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
php dersleri

Thanks.