为了账号安全,请及时绑定邮箱和手机立即绑定

将此作为维度数组

将此作为维度数组

PHP
守着一只汪 2022-06-17 15:41:29
我在制作维度数组时遇到问题,请帮助我使用 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'];等等,请帮忙 非常感谢
查看完整描述

3 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

    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>";


}

?>


查看完整回答
反对 回复 2022-06-17
?
MMMHUHU

TA贡献1834条经验 获得超8个赞

一个简单的循环将在这里执行:


<?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]

    ];

}


查看完整回答
反对 回复 2022-06-17
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

我认为@ajay-kumar 提供的array_map 解决方案是最好的,我会提出一个更动态和更易于阅读的解决方案


function bindDatas(&$resultSolution, $inputData, $keyName) 

{

    foreach ($inputData as $key => $value) {

        $resultSolution[$key][$keyName] = $value;

    }

}

一些人认为的测试代码是这样的:


//your code here

bindDatas($resultSolution, $arrayname, 'name');

bindDatas($resultSolution, $arrayage, 'age');

bindDatas($resultSolution, $arraygender, 'gender');

echo '<pre>';

print_r($resultSolution);

echo '</pre>';

确实,$key 变量引用了 $inputData 中的位置,但我们在这里使用它是因为我们希望保持全局数组中的顺序


查看完整回答
反对 回复 2022-06-17
  • 3 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信