Home > Article > Backend Development > How to parse array objects in php
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.
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:
<?php $arr = ['apple', 'banana', 'pear']; echo $arr[0]; //输出apple echo $arr[1]; //输出banana echo $arr[2]; //输出pear ?>
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.
<?php $arr = [ 'fruit' => [ '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'] . '元)<br/>'; } ?>
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.
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.
<?php class Person { public $name; public $age; public function sayHello() { echo 'Hello, I\'m ' . $this->name . '.'; } } $p = new Person(); $p->name = 'Jack'; $p->age = 18; $p->sayHello(); //输出Hello, I'm Jack. ?>
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.
<?php class Tag { public $name; public $value; } class Friend { public $name; public $contact; } class Person { public $name; public $age; public $tags = []; public $friend; }
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';
Next, we traverse all the information of each character and output it to the page.
<?php echo '<table border="1">'; echo '<tr><th>Name</th><th>Age</th><th>Tags</th><th>Friend</th></tr>'; foreach ([$p1, $p2] as $person) { echo '<tr>'; echo '<td>' . $person->name . '</td>'; echo '<td>' . $person->age . '</td>'; echo '<td>'; foreach ($person->tags as $tag) { echo $tag->name . ':' . $tag->value . '<br/>'; } echo '</td>'; echo '<td>' . $person->friend->name . '<br/>' . $person->friend->contact . '</td>'; echo '</tr>'; } echo '</table>'; ?>
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.
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.
<?php class Student { public $name; public $age; public $job; } $arr = [ new Student(), new Student(), new Student() ]; $arr[0]->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'; ?>
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.
<?php foreach ($arr as $item) { echo $item->name . '(' . $item->age . ',' . $item->job . ')<br/>'; } ?>
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.
<?php class Address { public $province; public $city; public $district; public $street; public $isDefault; } class Student { public $name; public $age; public $job; public $addresses = []; } $p = new Student(); $p->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; ?>
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.
<?php echo $p->name . '(' . $p->age . ',' . $p->job . ')<br/>'; foreach ($p->addresses as $addr) { echo $addr->province . $addr->city . $addr->district . $addr->street; echo '默认地址:' . ($addr->isDefault ? '是' : '否') . '<br/>'; } ?>
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!