简体   繁体   English

如何用PHP中的两个一维数组创建一个二维数组?

[英]How to create a 2-dimensional array from two 1-dimensional ones in PHP?

I have two arrays that I want to combine into one 2-dimensional array. 我有两个数组,我想合并成一个二维数组。

$firstnames = array("Jim", "Jack", "Mary", "Betty");

$lastnames = array("Brown", "Smith", "Hughes", "Malone");

I want an array $bothnames['firstname']['lastname'] , which I would like to echo and use to check against a PID field later on. 我想要一个数组$bothnames['firstname']['lastname'] ,我想在稍后回显并用于检查PID字段。

I've tried everything I know to populate the 2-dimensional array and echo it. 我已经尽我所知来填充二维数组并对其进行回显。

// populate

$bothnames = array();

for ($i=0; $i<count($firstnames); $i++)
 { 
    $bothnames[$i] = array();
        for ($x=0; $x<count($lastnames); $x++)
        {
           $bothnames[$i][$x] = $lastnames[$x];
        }
 }


// echo

for ($i=0; $i<count($firstnames); $i++)
 { 
   for ($x=0; $x<count($lastnames); $x++)
    {
    echo $bothnames[$i][$x]; 
    echo "<br>";
    }

  }

This just gives the last names (and loops 4 times). 这仅给出姓氏(并循环4次)。 How can I get the output 我如何获得输出

Jim Brown
Jack Smith
etc.

You can combine the arrays using the following method: 您可以使用以下方法合并数组:

<?php
$firstnames = array("Jim", "Jack", "Mary", "Betty");
$lastnames = array("Brown", "Smith", "Hughes", "Malone");
$names = array();
$i = 0;
foreach($firstnames as $fn)
{
    $names[$fn] = $lastnames[$i];
    $i++;
}
echo "<pre>";
print_r($names);

Using these later: 以后使用这些:

foreach($names as $first => $last)
{
    $firstName = $first;
    $lastName = $last;
}

It is to be noted that the length of both arrays should be same. 注意,两个阵列的长度应该相同。

Use the array_combine function 使用array_combine函数

$firstnames = array("Jim", "Jack", "Mary", "Betty");
$lastnames = array("Brown", "Smith", "Hughes", "Malone");
$bothnames = array_combine($firstnames, $lastnames);

foreach($bothnames as $key=>$value){
    echo $key." ".$value;
    echo "<br/>";
}

You should learn to utilize the power of PHP arrays. 您应该学习利用PHP数组的功能。 Here's an example: 这是一个例子:

$firstnames = array("Jim", "Jack", "Mary", "Betty");
$lastnames = array("Brown", "Smith", "Hughes", "Malone");
$bothnames = array_map(function ($firstname, $lastname) {
       return [$firstname,$lastname];
}, $firstnames, $lastnames);

foreach ($bothnames as $fullname) {
     echo $fullname[0]." ".$fullname[1];
}

Prints: 印刷品:

Jim Brown 吉姆·布朗

Jack Smith 杰克·史密斯

Mary Hughes 玛丽·休斯

Betty Malone 贝蒂·马龙

array_map details array_map详细信息

Use array_combine if you are sure that array has equal length 如果确定数组长度相等,请使用array_combine

$firstnames = ["Jim", "Jack", "Mary", "Betty"];
$lastnames = ["Brown", "Smith", "Hughes"];
$bothnames = array_combine($firstnames, $lastnames);

foreach ($bothnames as $key => $value) {
    echo $key . " " . $value;
    echo "\n";
}

or if you are not sure this code is more safe 或者如果您不确定此代码是否更安全

$firstnames = ["Jim", "Jack", "Mary", "Betty"];
$lastnames = ["Brown", "Smith", "Hughes", "Malone"];

foreach ($firstnames as $k => $v) {
    echo $v . (isset($lastnames[$k]) ? ' ' . $lastnames[$k] : '');
    echo "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM