gpt4 book ai didi

php - 是否有 PHP 函数可以将单独字段的数组连接成分组字段的数组?

转载 作者:行者123 更新时间:2023-12-03 23:04:52 24 4
gpt4 key购买 nike

是否有一个 PHP 函数可以接受多个数组(每个数组代表单个字段值的集合,例如名字)并输出一个数组,该数组的值将这些字段组合在一起?

例如:

$arr1 = ['John', 'Kyle']; // first names
$arr2 = ['Smith', 'Andrews']; // last names
$arr3 = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19b9e999fb19489909c819d94df929e9c" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec87958089ac89948d819c8089c28f8381" rel="noreferrer noopener nofollow">[email protected]</a>']; // email addresses
// ...

Output:

Array(
0 => ['John', 'Smith', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0cacfc8cee0c5d8c1cdd0ccc58ec3cfcd" rel="noreferrer noopener nofollow">[email protected]</a>'],
1 => ['Kyle', 'Andrews', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1cad8cdc4e1c4d9c0ccd1cdc48fc2cecc" rel="noreferrer noopener nofollow">[email protected]</a>'],
)

最佳答案

您可以使用array_map()以 null 作为第一个参数来合并它们...

NULL can be passed as a value to callback to perform a zip operation on multiple arrays. If only array1 is provided, array_map() will return the input array.

所以

print_r(array_map(null, $arr1, $arr2, $arr3));

给...

Array
(
[0] => Array
(
[0] => John
[1] => Smith
[2] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72181d1a1c32170a131f021e175c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>
)

[1] => Array
(
[0] => Kyle
[1] => Andrews
[2] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="402b392c25002538212d302c256e232f2d" rel="noreferrer noopener nofollow">[email protected]</a>
)

)

关于php - 是否有 PHP 函数可以将单独字段的数组连接成分组字段的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57628625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com