将此作为维度数组

我在制作维度数组时遇到问题,请帮助我使用 php,这是我的场景


$arrayname = array["john","mell","ben","henz","len"];

$arrayage = array["10","15","58","12","13"];

$arraygender = array["male","female","male","male","female"]; 

我想让它像


$arrayidentify = array['name' => 'john', 'age' => '10', 'gender' => 'male'];

等等,


请帮忙 非常感谢


守着一只汪
浏览 127回答 3
3回答

慕容708150

&nbsp; &nbsp; class Person {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public $name = "pname";&nbsp; &nbsp; &nbsp; &nbsp; public $age = "21";&nbsp; &nbsp; &nbsp; &nbsp; public $gender = "MaleOrFemale";&nbsp; &nbsp; &nbsp; &nbsp; function __construct($name,$age,$gender){&nbsp; &nbsp; &nbsp; &nbsp; $this->name = $name;&nbsp; &nbsp; &nbsp; &nbsp; $this->age = $age;&nbsp; &nbsp; &nbsp; &nbsp; $this->gender = $gender;&nbsp; &nbsp; }}您可以使用 OOP 方法并创建一个对象来存储一个人的数据并创建一个人员数组例子$steve = new Person("Steve Jobs","60","Male");&nbsp;这将创建一个名为 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&nbsp;class Person {&nbsp;&nbsp; &nbsp; public $name = "pname";&nbsp; &nbsp; public $age = "21";&nbsp; &nbsp; public $gender = "MaleOrFemale";&nbsp; &nbsp; function __construct($name,$age,$gender){&nbsp; &nbsp; $this->name = $name;&nbsp; &nbsp; $this->age = $age;&nbsp; &nbsp; $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){&nbsp; &nbsp; echo"Person Name : ".$person->name."<br>";&nbsp; &nbsp; echo"Person Age : ".$person->age."<br>";&nbsp; &nbsp; echo"Person Gender : ".$person->gender."<br><br>";}?>

MMMHUHU

一个简单的循环将在这里执行:<?php$names&nbsp; &nbsp;= ["john","mell","ben","henz","len"];$ages&nbsp; &nbsp; = ["10","15","58","12","13"];$genders = ["male","female","male","male","female"];&nbsp;for($i=0, $c=count($names), $out=[]; $i<$c; $i++) {&nbsp; &nbsp; $out[] = [&nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp;=> $names[$i],&nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; &nbsp; => $ages[$i],&nbsp; &nbsp; &nbsp; &nbsp; 'gender' => $genders[$i]&nbsp; &nbsp; ];}var_export($out);输出:array (&nbsp; &nbsp; 0 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'name' => 'john',&nbsp; &nbsp; &nbsp; 'age' => '10',&nbsp; &nbsp; &nbsp; 'gender' => 'male',&nbsp; &nbsp; ),&nbsp; &nbsp; 1 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'name' => 'mell',&nbsp; &nbsp; &nbsp; 'age' => '15',&nbsp; &nbsp; &nbsp; 'gender' => 'female',&nbsp; &nbsp; ),&nbsp; &nbsp; 2 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'name' => 'ben',&nbsp; &nbsp; &nbsp; 'age' => '58',&nbsp; &nbsp; &nbsp; 'gender' => 'male',&nbsp; &nbsp; ),&nbsp; &nbsp; 3 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'name' => 'henz',&nbsp; &nbsp; &nbsp; 'age' => '12',&nbsp; &nbsp; &nbsp; 'gender' => 'male',&nbsp; &nbsp; ),&nbsp; &nbsp; 4 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'name' => 'len',&nbsp; &nbsp; &nbsp; 'age' => '13',&nbsp; &nbsp; &nbsp; 'gender' => 'female',&nbsp; &nbsp; ),&nbsp; )您可以将 for 循环换成 foreach:foreach ($names as $k => $v) {&nbsp; &nbsp; $out[] = [&nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp;=> $names[$k],&nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; &nbsp; => $ages[$k],&nbsp; &nbsp; &nbsp; &nbsp; 'gender' => $genders[$k]&nbsp; &nbsp; ];}

胡说叔叔

我认为@ajay-kumar 提供的array_map 解决方案是最好的,我会提出一个更动态和更易于阅读的解决方案function bindDatas(&$resultSolution, $inputData, $keyName)&nbsp;{&nbsp; &nbsp; foreach ($inputData as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; $resultSolution[$key][$keyName] = $value;&nbsp; &nbsp; }}一些人认为的测试代码是这样的://your code herebindDatas($resultSolution, $arrayname, 'name');bindDatas($resultSolution, $arrayage, 'age');bindDatas($resultSolution, $arraygender, 'gender');echo '<pre>';print_r($resultSolution);echo '</pre>';确实,$key 变量引用了 $inputData 中的位置,但我们在这里使用它是因为我们希望保持全局数组中的顺序
打开App,查看更多内容
随时随地看视频慕课网APP