慕容708150
class Person { public $name = "pname"; public $age = "21"; public $gender = "MaleOrFemale"; function __construct($name,$age,$gender){ $this->name = $name; $this->age = $age; $this->gender = $gender; }}您可以使用 OOP 方法并创建一个对象来存储一个人的数据并创建一个人员数组例子$steve = new Person("Steve Jobs","60","Male"); 这将创建一个名为 steve 的人对象 $steve ,年龄 60 ,性别 Male您可以根据需要创建可能的人对象并将其放在数组中$jeff = new Person("Jeff Bezos","50","Male");$bill = new Person("Bill Gates","64","Male");将以上所有内容放在一个数组中,如下所示$persons = array($steve,$jeff,$bill);尝试下面的代码自学并理解它我使用过对象和构造函数在 w3schools.com 中了解它们<?php class Person { public $name = "pname"; public $age = "21"; public $gender = "MaleOrFemale"; function __construct($name,$age,$gender){ $this->name = $name; $this->age = $age; $this->gender = $gender;}}$steve = new Person("Steve Jobs","60","Male");$jeff = new Person("Jeff Bezos","50","Male");$bill = new Person("Bill Gates","64","Male");$persons = array($steve,$jeff,$bill);foreach($persons as $person){ echo"Person Name : ".$person->name."<br>"; echo"Person Age : ".$person->age."<br>"; echo"Person Gender : ".$person->gender."<br><br>";}?>
MMMHUHU
一个简单的循环将在这里执行:<?php$names = ["john","mell","ben","henz","len"];$ages = ["10","15","58","12","13"];$genders = ["male","female","male","male","female"]; for($i=0, $c=count($names), $out=[]; $i<$c; $i++) { $out[] = [ 'name' => $names[$i], 'age' => $ages[$i], 'gender' => $genders[$i] ];}var_export($out);输出:array ( 0 => array ( 'name' => 'john', 'age' => '10', 'gender' => 'male', ), 1 => array ( 'name' => 'mell', 'age' => '15', 'gender' => 'female', ), 2 => array ( 'name' => 'ben', 'age' => '58', 'gender' => 'male', ), 3 => array ( 'name' => 'henz', 'age' => '12', 'gender' => 'male', ), 4 => array ( 'name' => 'len', 'age' => '13', 'gender' => 'female', ), )您可以将 for 循环换成 foreach:foreach ($names as $k => $v) { $out[] = [ 'name' => $names[$k], 'age' => $ages[$k], 'gender' => $genders[$k] ];}