如何改造这个数组? [英] How transform this array?

查看:102
本文介绍了如何改造这个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是数据

$array = array(
    'random' => 1,
    'pewpew' => 2,
    'temp' => 5,
    'xoxo' => 3,
    'qweqweqe' => 4,
);

$fields = array('random', 'xoxo', 'temp');

我需要的结果得到:

I need to get in result:

$result = array(
    'random' => 1,
    'xoxo' => 3,
    'temp' => 5,
);

我的意思是从$领域适用于$数组键presence /订单。

I mean keys presence/order from $fields apply to $array.

现在的问题是:
我能使用仅array_功能这种转变? (我不想使用iteations)
如果是的话:你可以联系我的功能,我需要

The question is: Can I perform this transformation using only array_ functions? (I don't wanna use iteations) If yes: can you link me function that I need?

(抱歉拼写错误)

UPD。

PHP 5.2

推荐答案

这code preserves订购,并根据需要在PHP 5.2工作原理

This code preserves order and works in PHP 5.2 as required

一号线:

$result = array_merge( array_flip($fields),
                       array_intersect_key(
                                          $array,
                                          array_flip( $fields )
                                          )
                       );

有关性能:

$flip = array_flip($fields);
$result = array_merge( $flip
                       array_intersect_key(
                                          $array,
                                          $flip
                                          )
                       );

这篇关于如何改造这个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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