简体   繁体   English

如何将一个数组中的值作为键与另一个数组相关联?

[英]How to associate values from one array to another array as keys?

So, to simply put it, it looks like this: 所以,简单地说,它看起来像这样:

Array1: 数组1:

Array
(
    [0] => id
    [1] => name
    [2] => email
)

Array2: 数组2:

Array
(
    [0] => 1
    [1] => paula
    [2] => paula@paula.com

    [3] => 2
    [4] => martin
    [5] => martin@google.com

    [6] => 3
    [7] => kasandra
    [8] => kasandra@google.com

    [9] => 4
    [10] => helena
    [11] => helena@google.com

    [12] => 5
    [13] => sophia
    [14] => sophia@google.com

    [15] => 6
    [16] => denis
    [17] => denis@google.com
)

How to make those values from Array1 as keys in Array2 accordingly so that the end result looks like this: 如何将Array1中的值作为Array2中的键进行相应处理,以便最终结果如下所示:

Array
(
    [id] => 1
    [name] => paula
    [email] => paula@paula.com

    [id] => 2
    [name] => martin
    [email] => martin@google.com

    [id] => 3
    [name] => kasandra
    [email] => kasandra@google.com

    [id] => 4
    [name] => helena
    [email] => helena@google.com

    [id] => 5
    [name] => sophia
    [email] => sophia@google.com

    [id] => 6
    [name] => denis
    [email] => denis@google.com
)

you can use a for loop with step 3 您可以在步骤3中使用for循环

  $numCoords  = count($array2)/3
  for ($i = 0; $i < $numCoords; $i++ ){
    $array[$i]['id'] = $array2[$i*3]; 
    $array[$i]['name'] = $array2[($i*3)+1]; 
    $array[$i]['email'] = $array2[($i*3)+2]; 
  }

I would go with array_chunk and get used to numeric indexes, but if you really want to have them as words you may map them: 我会使用array_chunk并习惯于数字索引,但如果你真的想将它们作为单词,你可以映射它们:

$keys = [
    0 => 'id',
    1 => 'name',
    2 => 'email'
];

$chunked = array_chunk($array2, $length=3);
$result = array_map(function ($chunk) {
    global $keys;
    return array_combine($keys, $chunk);
}, $chunked);

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

相关问题 如何在同一键处将值从一个数组追加到另一个数组? - How to append values from one array to another array at same keys? 通过比较另一个数组的键从一个数组中删除数组值 - Removing Array Values From One Array By Comparing Keys Of Another Array 如何检查一个数组的值是否作为另一个数组的值或键存在? - How to check if values of one array exists as values or keys of another array? 如何在一个数组中获取多维关联数组键? - how to get multidimentional associate array keys in one array? 将键从一个数组匹配到另一个数组,并使用其值创建一个新数组? - Matching keys from one array to another and creating a new array with their values? PHP:如何使用另一个数组中的值覆盖一个数组中的值而不向数组添加新键? - PHP: How to overwrite values in one array with values from another without adding new keys to the array? PHP如何用另一个数组值替换一个关联数组键 - PHP How to replace one associative array keys with another array values 如何从键数组创建PHP关联数组? - How to create a PHP associate array from an array of keys? 如何将键数组与另一个数组的对应值组合? - How to combine an array of keys with corresponding values from another array? 如何从另一个数组的值构建PHP多维数组的键? - How to build the keys of a multidimensional array in PHP from the values of another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM