如何在php中合并三个数组

编程入门 行业动态 更新时间:2024-10-09 05:18:31
本文介绍了如何在php中合并三个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如,我有三个数组

Array1 array (size=3) 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'c' (length=1) Array2 array (size 3) 0 => string '$' (length=1) 1 => string '%' (length=1) 2 => string '^' (length=1) Array3 array (size 3) 0 => int '1' (length=1) 1 => int '2' (length=1) 2 => int '3' (length=1)

我希望array4具有这样的配置,即每个数组的每一行都应该是一组.例如

I want an array4 to be have this configuration that each array's each row should be one group. For instance

Array4 0th name=a type=$ price=1 1st name=b type=% price=2 2nd name=c type=^ price=3

我尝试了这个并给了我完美的结果

I tried this and gave me perfect result

$output = array_combine($array1, $array2);

但是当我尝试这样做时,不会给出预期的结果

But when I tried this it wouldn't give expected result

$output = array_combine(array_combine($array, $array2),$array3);

推荐答案

利用功能助手

通过使用 array_map 和 array_combine ,您可以跳过处理计数,迭代器,手动数组分配和手动数组合并之类的事情.

utilizing functional helpers

By using array_map and array_combine, you can skip having to handle things like counts, iterators, manual array assignment, and manual array combining.

此解决方案通过使用将所有复杂性隐藏在有用的功能性API之后的程序来降低复杂性.

This solution reduces complexity by using procedures which hide all that complexity behind useful, functional APIs.

$names = ['a', 'b', 'c']; $types = ['$', '%', '^']; $prices = [1, 2, 3]; $result = array_map(function ($name, $type, $price) { return array_combine( ['name', 'type', 'price'], [$name, $type, $price] ); }, $names, $types, $prices); echo json_encode($result, JSON_PRETTY_PRINT);

输出(为简洁起见,删除了一些换行符)

Output (some line-breaks removed for brevity)

[ {"name": "a", "type": "$", "price": 1}, {"name": "b", "type": "%", "price": 2}, {"name": "c", "type": "^", "price": 3} ]

隐藏自己的复杂性

您甚至可以通过定义自己的过程 array_zip_combine —来抽象出自己的复杂性.它的工作方式类似于 array_combine ,但是接受多个输入数组,这些数组在组合之前首先被压缩"

hiding your own complexity

You can even abstract away your own complexity by defining your own procedure, array_zip_combine — which works like array_combine but accepts multiple input arrays which are first "zipped" before being combined

此解决方案需要 PHP 7 +

// PHP 7 offers rest parameter and splat operator to easily apply variadic function function array_zip_combine (array $keys, ...$arrs) { return array_map(function (...$values) use ($keys) { return array_combine($keys, $values); }, ...$arrs); } $result = array_zip_combine(['name', 'type', 'price'], $names, $types, $prices);

与 PHP 5.4 +

// PHP 5 doesn't offer splat operator so we have to use // call_user_func_array to apply a variadic function function array_zip_combine (array $keys /* args */) { $f = function () use ($keys) { return array_combine($keys, func_get_args()); }; return call_user_func_array( 'array_map', array_merge([$f], array_slice(func_get_args(), 1)) ); } $result = array_zip_combine(['name', 'type', 'price'], $names, $types, $prices);

更多推荐

如何在php中合并三个数组

本文发布于:2023-11-29 10:00:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何在   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!