Converting JSON to PHP Array

  • Converting JSON to PHP Array – Converter JSON para Array com PHP

Converting JSON to PHP Array

To convert a JSON data string to a PHP array, you can use the json_decode($json) function. The json_decode() function accepts the JSON string as the first parameter and a few additional parameters to control the process of converting JSON to a PHP array. If the JSON cannot be converted to an array or the data nesting depth exceeds the recursion limit, the converting function will return NULL. In this JSON Convert to PHP Array example, we use the json_decode() function to convert JSON string to a PHP array. Click Execute to run the JSON to PHP Array Example online and see the result.

Converting JSON to PHP ArrayExecute

<?php
   $json = '{"UK": "London", "ES": "Madrid", "IT": "Rome"}';
   print_r (json_decode($json));
?>

Updated: Mar 10, 2023 Viewed: 5096 times 

 AuthorReqBin 

What is PHP?

PHP is a versatile, open-source server-side scripting language used for web development and can be embedded in HTML code. PHP is known for its ease of use, flexibility, and a large community of developers. PHP is compatible with many web servers and databases. PHP supports multiple databases such as MySQLPostgreSQL, and SQLite and can run on many operating systems, including Windows, Linux, and macOS.

What is an array in PHP?

An array in PHP is a variable that we use to store a data set containing various elements. The elements of an array are identified by a unique key or index, which can be a number or a string. PHP has two types of arrays: indexed arrays and associative arrays. PHP provides many built-in functions for manipulating arrays, including sorting, searching, checking if an element exists in an array, splitting string to array, converting array to string, converting array to JSON, and getting the length of an array.

What is JSON?

JSON (JavaScript Object Notation) is a language-independent text format for storing and exchanging data. JSON is widely used for web APIs, storing configuration data, exchanging data between web applications, and transmitting data between client and server in web applications. Many programming languages have built-in or external libraries for creating and manipulating JSON data strings, including JavaScriptPython, C++, C#, Go, PHP, and Java.

How to decode JSON string into PHP object?

You can use the json_decode() function to convert JSON strings into PHP objects. The result object can be any PHP data type, including an array, except for resource pointers such as a database or file descriptor.

JSON to PHP Array Converter ExampleExecute

<?php
$json = '{"UK": "London", "ES": "Madrid", "IT": "Rome"}';

print_r (json_decode($json));
?>

#output: stdClass Object
(
    [UK] => London
    [ES] => Madrid
    [IT] => Rome
)

Converting JSON to PHP Array Examples

The following are examples of converting JSON to an array in PHP:

Converting JSON Object to Array

A JSON object in PHP is an associative array where the keys are strings, and the values are any valid data types, including other JSON objects and arrays. The following is an example of converting a JSON object to a PHP array using the json_decode() function:

Converting JSON Object to Array ExampleExecute

<?php
$json = '{"Status": "admin", "Name": "Leo", "Age": 28}';

print_r (json_decode($json));
?>

#output: stdClass Object
(
    [Status] => admin
    [Name] => Leo
    [Age] => 28
)
Converting JSON Array to Array

JSON array array in PHP is a data structure that allows you to store an ordered set of values in JSON format. The following is an example of converting a JSON array to a PHP array using the json_decode() function:

Converting JSON Array to Array ExampleExecute

<?php
$json = '["JSON", "to", "PHP", "Array"]';

print_r (json_decode($json));
?>

#output: Array
(
    [0] => JSON
    [1] => to
    [2] => PHP
    [3] => Array
)
Converting Nested JSON Object to Array

A nested JSON Object in PHP is a JSON object that includes other objects or arrays. In PHP, such an object can be created using a multidimensional array. The following is an example of nesting a JSON object into a PHP array using the json_decode() function:

Converting Nested JSON Object to Array ExampleExecute

<?php
$json = '{"name": "Leo", "age": 28, "address": {"country": "USA", "city": "New York"}}';

print_r (json_decode($json));
?>

#output: stdClass Object
(
    [name] => Leo
    [age] => 28
    [address] => stdClass Object
        (
            [country] => USA
            [city] => New York
        )

)

See also

FONTE DE MAIS PHP CAST E CONVERSÃO DE OBJETOS: https://reqbin.com/code/php/0uwqq41y/json-to-array-php-example

Please follow and like us:
Pin Share

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

RSS
Follow by Email
WP Radio
WP Radio
OFFLINE LIVE