通过索引结合PHP数组 [英] Combining PHP array by index

查看:109
本文介绍了通过索引结合PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个数组。我需要在PHP的方式将它们结合在一起,使得这些值在索引[I]从两个阵列被添加到相同的索引的最终数组中为止。

阵列1

  [0] => '123456'
[1] => '654123'
[2] => '987456'
[3] => '489522'
[4] => '014779'

阵列2

  [0] => '特征'
[1] => 促销
[2] => '其他'
[3] => '开始'
[4] => '结束'

最后一个数组我需要

  [0] => ['123456','功能']
[1] => ['654123','促销']
[2] => ['987456','其他']
[3] => ['489522','开始']
[4] => ['014779','结束']


解决方案

目前最简单的方法是使用的 array_map

  $结果= array_map(NULL,$数组1,$数组2));

看到它在行动

I have the following two arrays. I need a way in PHP to combine them together so that the values at index[i] from both arrays are added to the same index in the final array.

Array 1

[0] => '123456'
[1] => '654123'
[2] => '987456'
[3] => '489522'
[4] => '014779'

Array 2

[0] => 'feature'
[1] => 'promo'
[2] => 'other'
[3] => 'start'
[4] => 'end'

Final Array I need

[0] => ['123456', 'feature']
[1] => ['654123', 'promo']
[2] => ['987456', 'other']
[3] => ['489522', 'start']
[4] => ['014779', 'end']

解决方案

By far the easiest way is to use array_map:

$result = array_map(null, $array1, $array2));

See it in action.

这篇关于通过索引结合PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆