Home  >  Article  >  Backend Development  >  How to get the number of elements of an array in json in php

How to get the number of elements of an array in json in php

PHPz
PHPzOriginal
2023-04-26 14:22:001187browse

For PHP developers, processing JSON data is a very basic task, and getting the number of an array element in JSON data is also a very common need. Through this article, you will learn how to get the number of array elements in JSON data in PHP, as well as some common functions and techniques.

  1. What is JSON?

Before introducing how to get the number of array elements in JSON data, we need to first understand what JSON is. JSON (JavaScript Object Notation) is a lightweight data exchange format derived from JavaScript object notation. JSON data is designed to represent a simple data structure that is readable, easy to parse and generate.

In PHP, you can use the built-in functions json_encode() and json_decode() to encode and decode JSON data into PHP objects or arrays respectively. Therefore, PHP developers can easily process JSON data and perform a series of operations.

  1. Get the number of JSON array elements

Getting the number of JSON array elements in PHP is very simple. You can use the count() function and json_decode() function. Features to operate.

The following is a sample JSON data:

{
  "fruits": [
    { "name": "apple", "color": "red" },
    { "name": "banana", "color": "yellow" },
    { "name": "orange", "color": "orange" }
  ]
}

If you want to get the number of elements in the fruits array, you can first use the json_decode() function to decode the JSON data into a PHP object or array, and then Use the count() function to get the number of array elements. The sample code is as follows:

$json = '{"fruits":[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"},{"name":"orange","color":"orange"}]}';

$data = json_decode($json, true);

$fruitsCount = count($data['fruits']);

echo $fruitsCount;  // 输出:3

In the above example, a JSON string is first declared, and then the json_decode() function is used to decode it into a PHP array and save it to the variable $data. Next, use the count() function and the array key 'fruits' to get the number of elements in the fruits array. Finally, the results are output to the console.

It should be noted that if the second parameter is set to true when using the json_decode() function, a PHP array will be returned, otherwise a PHP object will be returned. In this example, since we want to use the count() function to get the number of array elements, we set the second parameter to true.

  1. Commonly used JSON data processing functions

In PHP, there are many commonly used functions that can be used to process JSON data. Here are some more commonly used functions and techniques. And illustrate with examples.

(1) json_encode()

json_encode() function is used to encode PHP arrays or objects into JSON format strings. The example is as follows:

$data = array(
    'name' => 'John',
    'age' => 30,
    'email' => 'john@example.com'
);

$json = json_encode($data);

echo $json;

The output result is:

{"name":"John","age":30,"email":"john@example.com"}

(2) json_decode()

json_decode() function is used to decode the JSON format string into a PHP array or object . Examples are as follows:

$json = '{"name":"John","age":30,"email":"john@example.com"}';

$data = json_decode($json, true);

echo $data['name'];  // 输出:John

(3) isset() / empty()

isset() function is used to determine whether a variable has been set, and empty() function is used to determine whether a variable has been set. Is empty. When processing JSON data, it is often necessary to use these two functions for judgment. Examples are as follows:

$json = '{"name":"John","age":30}';

$data = json_decode($json, true);

if (isset($data['name'])) {
    echo $data['name'];
}

if (!empty($data['age'])) {
    echo $data['age'];
}

(4) array_push()

array_push() function pushes one or more elements to the end of the array. When processing JSON data, you can use this function if you need to add elements to an array. The example is as follows:

$json = '{"fruits":[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"}]}';

$data = json_decode($json, true);

array_push($data['fruits'], array('name' => 'orange', 'color' => 'orange'));

echo json_encode($data);

The output result is:

{"fruits":[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"},{"name":"orange","color":"orange"}]}

In the above example, use the array_push() function to push a new element to the end of the fruits array, and encode the modified array as JSON string, finally output to the console.

(5) array_column()

array_column() function is used to get all the values ​​of the specified key from a two-dimensional array and return a one-dimensional array. When processing JSON data, if you want to get all the values ​​corresponding to a certain key in an array, you can use this function. The example is as follows:

$json = '{"fruits":[{"name":"apple","color":"red"},{"name":"banana","color":"yellow"},{"name":"orange","color":"orange"}]}';

$data = json_decode($json, true);

$names = array_column($data['fruits'], 'name');

print_r($names);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

In the above example, the 'name' attribute of all elements in the fruits array is obtained through the array_column() function, and a one-dimensional array is returned. and output to the console.

  1. Summary

In PHP, processing JSON data is an unavoidable task. This article introduces how to obtain the number of array elements in JSON data, as well as some commonly used JSON data processing functions and techniques. After studying this article, you should be able to process JSON data more skillfully and realize your business needs.

The above is the detailed content of How to get the number of elements of an array in json in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn