How to parse array objects in php

PHPz
Release: 2023-04-24 15:36:32
Original
899 people have browsed it

When developing web applications, arrays and objects are one of the most commonly used data structures. As a scripting language, PHP is very convenient for handling arrays and objects. However, when we encounter data with complex structures such as multi-dimensional arrays, arrays containing objects, objects containing arrays, etc., PHP parsing requires some skills and methods.

This article will introduce how PHP parses complex structural data such as arrays, objects, nested objects in arrays, and nested arrays in objects. It will illustrate the parsing process and precautions through some examples.

  1. Parsing Arrays

Arrays in PHP can be created directly using the keyword array or through the [] symbol. The parsing of simple arrays is very simple. You can directly access the elements in the array through subscripts, such as:

Copy after login

However, when the array becomes complex and contains multi-dimensional arrays, accessing the elements is not so easy. At this point, we need to use traversal to parse the elements in the array.

 [
        'apple' => ['color' => 'red', 'price' => 2],
        'banana' => ['color' => 'yellow', 'price' => 3],
        'pear' => ['color' => 'green', 'price' => 4]
    ]
];

foreach ($arr['fruit'] as $name => $item) {
    echo $name . ':' . $item['color'] . '(' . $item['price'] . '元)
'; } ?>
Copy after login

In the above code, we use a foreach loop to traverse the elements in the array and obtain the color and price of each fruit in the array. This traversal method is suitable for any multi-level array structure.

  1. Parsing objects

In PHP, we can use the keywords class and new to create objects. The parsing of simple objects is very simple, and you can use the arrow operator -> to directly access the properties or methods of the object.

name . '.';
    }
}

$p = new Person();
$p->name = 'Jack';
$p->age = 18;
$p->sayHello(); //输出Hello, I'm Jack.
?>
Copy after login

However, when the object contains an inner array or multiple objects, accessing the elements requires a more complex traversal method.

For example, we create a "character" object. Each character contains an array of "labels" and a "friend" object. The friend object contains two "name" and "contact information" Attributes.

Copy after login

We create two character objects, each object contains two tags and a friend.

$p1 = new Person();
$p1->name = 'Jack';
$p1->age = 18;
$p1->tags[0] = new Tag();
$p1->tags[0]->name = 'sport';
$p1->tags[0]->value = 5;
$p1->tags[1] = new Tag();
$p1->tags[1]->name = 'music';
$p1->tags[1]->value = 3;
$p1->friend = new Friend();
$p1->friend->name = 'Lucas';
$p1->friend->contact = '123456';

$p2 = new Person();
$p2->name = 'Amy';
$p2->age = 20;
$p2->tags[0] = new Tag();
$p2->tags[0]->name = 'reading';
$p2->tags[0]->value = 3;
$p2->tags[1] = new Tag();
$p2->tags[1]->name = 'travel';
$p2->tags[1]->value = 2;
$p2->friend = new Friend();
$p2->friend->name = 'Lily';
$p2->friend->contact = '654321';
Copy after login

Next, we traverse all the information of each character and output it to the page.

';
echo 'NameAgeTagsFriend';

foreach ([$p1, $p2] as $person) {
    echo '';
    echo '' . $person->name . '';
    echo '' . $person->age . '';
    echo '';
    foreach ($person->tags as $tag) {
        echo $tag->name . ':' . $tag->value . '
';     }     echo '';     echo '' . $person->friend->name . '
' . $person->friend->contact . '';     echo ''; } echo ''; ?>
Copy after login

In the above code, we use two foreach loops, one to traverse the array of characters, and the other to traverse the array of labels within the characters. At the same time, we use the arrow operator -> to obtain and output friend information.

  1. Parse the objects in the array

When we want to process an array and each element in the array is an object, we need to use traversal to parse the array elements, and use the arrow operator -> to get the properties within each object.

For example, we create an array to store student information. Each student has three attributes: name, age and occupation.

name = 'Lucas';
$arr[0]->age = 18;
$arr[0]->job = 'Student';

$arr[1]->name = 'Lily';
$arr[1]->age = 19;
$arr[1]->job = 'Engineer';

$arr[2]->name = 'Jack';
$arr[2]->age = 20;
$arr[2]->job = 'Teacher';
?>
Copy after login

We use a foreach loop to traverse each element in the array, and use the arrow operator -> to obtain and output the three attributes of the student.

name . '(' . $item->age . ',' . $item->job . ')
'; } ?>
Copy after login
  1. Parse the array in the object

When we want to process an object and an attribute in the object is an array, we need to use traversal to parse the array element, and use the arrow operator -> to obtain other properties within the object.

For example, we create a student object. The student has a name, age, occupation and multiple addresses. An address contains province, city, district, street information, and a Boolean attribute indicating whether it is the default address.

name = 'Lucas';
$p->age = 18;
$p->job = 'Student';

$p->addresses[] = new Address();
$p->addresses[0]->province = '江苏省';
$p->addresses[0]->city = '南京市';
$p->addresses[0]->district = '鼓楼区';
$p->addresses[0]->street = '汉口路1号';
$p->addresses[0]->isDefault = true;

$p->addresses[] = new Address();
$p->addresses[1]->province = '江苏省';
$p->addresses[1]->city = '南京市';
$p->addresses[1]->district = '玄武区';
$p->addresses[1]->street = '北京东路1号';
$p->addresses[1]->isDefault = false;
?>
Copy after login

We traverse the address array within the student object, use the arrow operator -> to obtain other attributes of the student, and output them to the page.

name . '(' . $p->age . ',' . $p->job . ')
'; foreach ($p->addresses as $addr) {     echo $addr->province . $addr->city . $addr->district . $addr->street;     echo '默认地址:' . ($addr->isDefault ? '是' : '否') . '
'; } ?>
Copy after login

To sum up, PHP needs to use different methods according to the actual situation to parse complex structural data such as arrays, objects, nested objects in arrays, and nested arrays in objects, by traversing and accessing properties. Analytical data. When processing complex data, we need to choose an appropriate parsing method based on the structure of the data and usage scenarios.

The above is the detailed content of How to parse array objects in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!