Home  >  Article  >  Backend Development  >  How to put array in php object

How to put array in php object

WBOY
WBOYOriginal
2023-05-11 10:42:07651browse

In PHP objects, properties and methods can be stored through arrays. Objects are defined by classes, which are collections of properties and methods. In PHP, an object's properties and methods are accessed through the object's name and an arrow symbol (->). So how do PHP objects put arrays? Let’s learn about it step by step.

  1. Creating objects and arrays

First, we need to create an object and an array in order to store the array in the object. The following code will create an object named "person" and an array named "person_info".

//创建person对象
$person = new stdClass();

//创建person_info数组
$person_info = array(
    'name' => '张三',
    'age' => 25,
    'gender' => '男'
);
  1. Assign the array to the object's properties

Next, we can assign the array to the object's properties. In PHP, the properties of an object can be public, protected or private, and we can define them using the corresponding access modifiers.

//将person_info数组赋给person对象的属性
$person->info = $person_info;

At this time, the attribute "info" of the $person object is an array containing key-value pairs of "name", "age" and "gender".

  1. Accessing the array in the object

We can access the array in the object using the name of the object property and the arrow symbol. The following code demonstrates how to access the "name" property in the $person object.

echo $person->info['name']; //输出"张三"

Note that we need to use the array access symbol "[]" to access the properties in the object, rather than the dot operation symbol "."

  1. Use object methods to operate arrays

We can also define methods in objects to operate arrays. The code below shows how to define a method in an object to get the number of people aged 18 and above.

class Person {
    public $info;
    
    public function getAdultCount() {
        $count = 0;
        foreach($this->info as $item) {
            if($item['age'] >= 18) {
                $count++;
            }
        }
        return $count;
    }
}

$person = new Person();
$person->info = array(
    array('name'=>'张三', 'age'=>25, 'gender'=>'男'),
    array('name'=>'李四', 'age'=>16, 'gender'=>'女'),
    array('name'=>'王五', 'age'=>21, 'gender'=>'男'),
);

echo $person->getAdultCount(); //输出"2"

In the above code, we define a method named "getAdultCount", in which each element in the "info" attribute of the $person object is traversed, and if its age is greater than or equal to 18 years, a counter is incremented. Finally, the value of the counter is returned.

In this way, we can use arrays in objects and define corresponding methods to operate on them.

Summary

In PHP objects, we can store properties and methods through arrays. We need to create an object and an array and assign the array to the object's properties. We can access the array in the object using the name of the object's properties and arrow symbols, or we can define methods in the object to operate on the array. In this way, we can use arrays in PHP more flexibly and conveniently.

The above is the detailed content of How to put array in php object. 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