Làm cách nào để truy cập dữ liệu được giải mã JSON trong PHP?

Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách thực hiện thao tác phổ biến nhất, chẳng hạn như mã hóa, giải mã và chuyển đổi JSON thành Mảng và Mảng thành JSON với sự trợ giúp của các ví dụ

Chúng ta cũng sẽ khám phá cách đọc và ghi tệp JSON trong PHP. Cuối cùng, chúng ta sẽ xem cách truyền một tệp JSON lớn trong khi sử dụng rất ít bộ nhớ

Toàn bộ có sẵn trong kho GitHub;

Ví dụ chuỗi JSON

Trong hướng dẫn này, chúng ta sẽ sử dụng chuỗi JSON sau chứa danh sách phim và thuộc tính của chúng

[
{
"id": "287947",
"title": "Shazam!",
"poster": "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date": "1553299200",
"genres": [
"Action",
"Comedy",
"Fantasy"
]
},
{
"id": "299537",
"title": "Captain Marvel",
"poster": "https://image.tmdb.org/t/p/w500/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg",
"overview": "The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.",
"release_date": "1551830400",
"genres": [
"Action",
"Adventure",
"Science Fiction"
]
}
]

Chuyển đổi một chuỗi JSON thành một mảng kết hợp bằng cách sử dụng json_decode

Chúng tôi sử dụng

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
4 để chuyển đổi (giải mã) chuỗi thành một mảng kết hợp PHP

Tham số thứ hai của

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
4 chỉ định xem chúng ta muốn lấy từng đối tượng JSON dưới dạng một mảng kết hợp hay dưới dạng một đối tượng. Ở đây, chúng tôi chọn tùy chọn mảng kết hợp

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);

Kết quả là một mảng chứa một mảng kết hợp cho mỗi phim. Các khóa của mỗi mảng kết hợp là các trường của đối tượng JSON

array(2) {
[0]=>
array(6) {
["id"]=>
string(6) "287947"
["title"]=>
string(7) "Shazam!"
["poster"]=>
string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
["overview"]=>
string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
["release_date"]=>
string(10) "1553299200"
["genres"]=>
array(3) {
[0]=>
string(6) "Action"
[1]=>
string(6) "Comedy"
[2]=>
string(7) "Fantasy"
}
}
[1]=> // .. the second movie
}

Chuyển đổi một mảng liên kết thành một chuỗi JSON bằng cách sử dụng json_encode

Chúng tôi sử dụng

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
6 để chuyển đổi (mã hóa) mảng kết hợp thành chuỗi JSON

Tham số thứ hai của

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
6 chỉ định định dạng của chuỗi. Chúng ta có thể sử dụng tùy chọn nhỏ gọn theo mặc định. Chúng tôi cũng có thể sử dụng định dạng đẹp;

$jsonArray = [
[
"id" => "287947",
"title" => "Shazam!",
"poster" => "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview" => "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date" => "1553299200",
"genres"=> ["Action", "Comedy", "Fantasy"]
],
[
"id" => "299537",
"title" => "Captain Marvel",
"poster" => "https://image.tmdb.org/t/p/w500/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg",
"overview" => "The story follows Carol Danvers as she becomes one of the universe’s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.",
"release_date" => "1551830400",
"genres"=> ["Action", "Adventure", "Science Fiction"]
]
];
// compact format
$compactJsonString = json_encode($jsonArray);
echo $compactJsonString.PHP_EOL;
// pretty human-readable format
$prettyJsonString = json_encode($jsonArray, JSON_PRETTY_PRINT);
echo $prettyJsonString.PHP_EOL;

Định dạng chuỗi JSON nhỏ gọn

________số 8_______

Định dạng chuỗi JSON đẹp

[
{
"id": "287947",
"title": "Shazam!",
"poster": "https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date": "1553299200",
"genres": [
"Action",
"Comedy",
"Fantasy"
]
},
{
"id": "299537",
"title": "Captain Marvel",
"poster": "https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg",
"overview": "The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.",
"release_date": "1551830400",
"genres": [
"Action",
"Adventure",
"Science Fiction"
]
}
]

Đọc tệp JSON trong PHP

Chúng tôi sẽ sử dụng cùng một định dạng ở đây;

[
{
"id": "287947",
"title": "Shazam!",
"poster": "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date": "1553299200",
"genres": [
"Action",
"Comedy",
"Fantasy"
]
},
{
"id": "299537",
"title": "Captain Marvel",
"poster": "https://image.tmdb.org/t/p/w500/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg",
"overview": "The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.",
"release_date": "1551830400",
"genres": [
"Action",
"Adventure",
"Science Fiction"
]
},
// .. and more lines
]

Chúng tôi phân tích tệp JSON bằng các hàm PHP gốc

  1. Mở và đọc nội dung của tệp bằng
    $jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
    $jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
    var_dump($jsonArray);
    9
  2. Chuyển đổi chuỗi JSON thành một mảng kết hợp với
    array(2) {
    [0]=>
    array(6) {
    ["id"]=>
    string(6) "287947"
    ["title"]=>
    string(7) "Shazam!"
    ["poster"]=>
    string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
    ["overview"]=>
    string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
    ["release_date"]=>
    string(10) "1553299200"
    ["genres"]=>
    array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(6) "Comedy"
    [2]=>
    string(7) "Fantasy"
    }
    }
    [1]=> // .. the second movie
    }
    0
  3. In mảng kết hợp với
    array(2) {
    [0]=>
    array(6) {
    ["id"]=>
    string(6) "287947"
    ["title"]=>
    string(7) "Shazam!"
    ["poster"]=>
    string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
    ["overview"]=>
    string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
    ["release_date"]=>
    string(10) "1553299200"
    ["genres"]=>
    array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(6) "Comedy"
    [2]=>
    string(7) "Fantasy"
    }
    }
    [1]=> // .. the second movie
    }
    1

$path = 'data/movies-10.json';
$jsonString = file_get_contents($path);
$jsonData = json_decode($jsonString, true);
var_dump($jsonData);

Kết quả

array(10) {
[0]=>
array(6) {
["id"]=>
string(6) "287947"
["title"]=>
string(7) "Shazam!"
["poster"]=>
string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
["overview"]=>
string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
["release_date"]=>
string(10) "1553299200"
["genres"]=>
array(3) {
[0]=>
string(6) "Action"
[1]=>
string(6) "Comedy"
[2]=>
string(7) "Fantasy"
}
}
// .. and more lines
}

Viết tệp JSON bằng PHP

Chúng tôi tạo và viết một tệp JSON từ một mảng kết hợp

  1. Chuyển đổi danh sách mảng kết hợp thành chuỗi JSON với
    array(2) {
    [0]=>
    array(6) {
    ["id"]=>
    string(6) "287947"
    ["title"]=>
    string(7) "Shazam!"
    ["poster"]=>
    string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
    ["overview"]=>
    string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
    ["release_date"]=>
    string(10) "1553299200"
    ["genres"]=>
    array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(6) "Comedy"
    [2]=>
    string(7) "Fantasy"
    }
    }
    [1]=> // .. the second movie
    }
    2
  2. Mở tệp JSON mới bằng
    array(2) {
    [0]=>
    array(6) {
    ["id"]=>
    string(6) "287947"
    ["title"]=>
    string(7) "Shazam!"
    ["poster"]=>
    string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
    ["overview"]=>
    string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
    ["release_date"]=>
    string(10) "1553299200"
    ["genres"]=>
    array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(6) "Comedy"
    [2]=>
    string(7) "Fantasy"
    }
    }
    [1]=> // .. the second movie
    }
    3
  3. Viết chuỗi JSON bằng
    array(2) {
    [0]=>
    array(6) {
    ["id"]=>
    string(6) "287947"
    ["title"]=>
    string(7) "Shazam!"
    ["poster"]=>
    string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
    ["overview"]=>
    string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
    ["release_date"]=>
    string(10) "1553299200"
    ["genres"]=>
    array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(6) "Comedy"
    [2]=>
    string(7) "Fantasy"
    }
    }
    [1]=> // .. the second movie
    }
    4
  4. Đóng tệp bằng
    array(2) {
    [0]=>
    array(6) {
    ["id"]=>
    string(6) "287947"
    ["title"]=>
    string(7) "Shazam!"
    ["poster"]=>
    string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
    ["overview"]=>
    string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
    ["release_date"]=>
    string(10) "1553299200"
    ["genres"]=>
    array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(6) "Comedy"
    [2]=>
    string(7) "Fantasy"
    }
    }
    [1]=> // .. the second movie
    }
    5

$path = 'data/new-file.json';
// JSON data as an array
$jsonData = [
[
"id" => "287947",
"title" => "Shazam!",
"poster" => "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
"overview" => "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
"release_date" => "1553299200",
"genres"=> ["Action", "Comedy", "Fantasy"]
],
[
"id" => "299537",
"title" => "Captain Marvel",
"poster" => "https://image.tmdb.org/t/p/w500/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg",
"overview" => "The story follows Carol Danvers as she becomes one of the universe’s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.",
"release_date" => "1551830400",
"genres"=> ["Action", "Adventure", "Science Fiction"]
]
];
// Convert JSON data from an array to a string
$jsonString = json_encode($jsonData, JSON_PRETTY_PRINT);
// Write in the file
$fp = fopen($path, 'w');
fwrite($fp, $jsonString);
fclose($fp);

CSV cho các giá trị được phân tách bằng dấu phẩy là một định dạng tiêu chuẩn khác để trao đổi dữ liệu giữa các ứng dụng và khá đơn giản đối với (và từ CSV sang JSON)

hết bộ nhớ

Khi đọc một tệp JSON lớn, bạn sẽ nhanh chóng gặp sự cố tiêu thụ bộ nhớ. "Lỗi. Kích thước bộ nhớ cho phép của X byte đã cạn kiệt"

Nó bình thường. Các phương thức gốc tải toàn bộ nội dung trong bộ nhớ. Bạn có thể tăng

array(2) {
[0]=>
array(6) {
["id"]=>
string(6) "287947"
["title"]=>
string(7) "Shazam!"
["poster"]=>
string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
["overview"]=>
string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
["release_date"]=>
string(10) "1553299200"
["genres"]=>
array(3) {
[0]=>
string(6) "Action"
[1]=>
string(6) "Comedy"
[2]=>
string(7) "Fantasy"
}
}
[1]=> // .. the second movie
}
6 cho đến khi đạt đến giới hạn bộ nhớ của máy chủ của bạn. Một số tệp quá lớn nên sẽ không đủ

Tin tuyệt vời, có một cách tiếp cận khác;

Truyền tệp JSON lớn

Chúng tôi sử dụng tệp JSON bao gồm 500 nghìn bản ghi phim;

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
0

Chúng tôi sẽ cài đặt và sử dụng thư viện

array(2) {
[0]=>
array(6) {
["id"]=>
string(6) "287947"
["title"]=>
string(7) "Shazam!"
["poster"]=>
string(63) "https://image.tmdb.org/t/p/w500/xnopI5Xtky18MPhK40cZAGAOVeV.jpg"
["overview"]=>
string(98) "A boy is given the ability to become an adult superhero in times of need with a single magic word."
["release_date"]=>
string(10) "1553299200"
["genres"]=>
array(3) {
[0]=>
string(6) "Action"
[1]=>
string(6) "Comedy"
[2]=>
string(7) "Fantasy"
}
}
[1]=> // .. the second movie
}
7

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
1

JSON Machine là một trình phân tích cú pháp luồng JSON PHP hiệu quả dựa trên các trình tạo

Đọc tệp JSON lớn của chúng tôi

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
2

và đây là kết quả

$jsonString = '[{"id":"287947","title":"Shazam!","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/xnopI5Xtky18MPhK40cZAGAOVeV.jpg","overview":"A boy is given the ability to become an adult superhero in times of need with a single magic word.","release_date":"1553299200","genres":["Action","Comedy","Fantasy"]},{"id":"299537","title":"Captain Marvel","poster":"https:\/\/image.tmdb.org\/t\/p\/w500\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","overview":"The story follows Carol Danvers as she becomes one of the universe\u2019s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s, Captain Marvel is an all-new adventure from a previously unseen period in the history of the Marvel Cinematic Universe.","release_date":"1551830400","genres":["Action","Adventure","Science Fiction"]}]';
$jsonArray = json_decode($jsonString, true); // true for associative arrays, false for objects
var_dump($jsonArray);
3

Có, chúng tôi đã phân tích cú pháp 500 nghìn bản ghi JSON của mình trong 20 giây và chỉ sử dụng 8 MB

Chúng tôi không thể dự đoán dung lượng của tệp cần tích hợp trong một số trường hợp. Sử dụng phương pháp phát trực tuyến sẽ làm cho mã của bạn mạnh mẽ hơn, nhanh hơn, tiết kiệm bộ nhớ hơn và không bị ảnh hưởng trong tương lai

Làm cách nào để hiển thị dữ liệu giải mã JSON trong PHP?

PHP và JSON .
Hàm json_encode() dùng để mã hóa một giá trị sang định dạng JSON
Hàm json_decode() được sử dụng để giải mã một đối tượng JSON thành một đối tượng PHP hoặc một mảng kết hợp
Hàm json_decode() trả về một đối tượng theo mặc định. .
Bạn cũng có thể lặp qua các giá trị bằng vòng lặp foreach()

Làm cách nào để truy xuất dữ liệu JSON trong PHP?

Đọc JSON từ tệp hoặc chuỗi trong PHP . Khi dữ liệu ở dạng chuỗi, bạn có thể gọi hàm json_decode() để trích xuất thông tin từ chuỗi. get the data from the file into a variable by using file_get_contents() . Once the data is in a string, you can call the json_decode() function to extract information from the string.

Làm cách nào để truy cập biến JSON trong PHP?

Để nhận chuỗi JSON, chúng ta có thể sử dụng “php. //input” cùng với hàm file_get_contents() giúp chúng ta nhận dữ liệu JSON dưới dạng file và đọc thành chuỗi. Sau này, chúng ta có thể sử dụng hàm json_decode() để giải mã chuỗi JSON.

Đầu ra của json_decode trong PHP là gì?

Hàm json_decode() có thể trả về một giá trị được mã hóa trong JSON ở loại PHP thích hợp . Các giá trị true, false và null được trả về lần lượt là TRUE, FALSE và NULL. NULL được trả về nếu không thể giải mã JSON hoặc nếu dữ liệu được mã hóa sâu hơn giới hạn đệ quy.