如何将数组或对象的数组转换为关联数组? [英] How to convert an array of arrays or objects to an associative array?

查看:179
本文介绍了如何将数组或对象的数组转换为关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用perl的map()函数,其中回调函数可以分配键和值,从而创建一个关联数组,其中输入是平面数组。我知道 array_fill_keys() ,如果您要做的只是创建字典式哈希,这很有用,但是如果您不一定希望所有值都相同怎么办?显然,所有的事情都可以通过foreach迭代来完成,但是还有其他(可能更优雅)的方法吗?

I'm used to perl's map() function where the callback can assign both the key and the value, thus creating an associative array where the input was a flat array. I'm aware of array_fill_keys() which can be useful if all you want to do is create a dictionary-style hash, but what if you don't necessarily want all values to be the same? Obviously all things can be done with foreach iteration, but what other (possibly more elegant) methods exist?

编辑:添加示例以阐明转型。请不要挂在转换上,问题在于将平面列表转换成哈希,在这里我们不能假设所有值都相同。

Edit: adding an example to clarify the transformation. Please don't get hung up on the transformation, the question is about transforming a flat list to a hash where we can't assume that all the values will be the same.

$original_array: ('a', 'b', 'c', 'd')
$new_hash: ('a'=>'yes', 'b'=>'no', 'c'=>'yes', 'd'=>'no')

*note: the values in this example are arbitrary, governed by some business logic that is not really relevant to this question. For example, perhaps it's based on the even-oddness of the ordinal value of the key

的序数值的奇偶性实际示例
因此,使用此处提供的答案,您可以通过$ _POST进行解析,以获取仅包含符合指定条件的那些输入字段的列表。例如,如果您的表单中有很多输入字段,但是必须一起处理其中的某些输入字段,这可能会很有用。

Real-world Example So, using an answer that was provided here, here is how you could parse through the $_POST to get a list of only those input fields that match a given criteria. This could be useful, for example, if you have a lot of input fields in your form, but a certain group of them must be processed together.

在这种情况下,我有许多输入字段,它们代表到数据库的映射。每个输入字段如下所示:
<输入名称= field-user_email value = 2 />

In this case I have a number of input fields that represent mappings to a database. Each of the input fields looks like this: <input name="field-user_email" value="2" /> where each of this type of field is prefixed with "field-".

我们要做的是,首先获取仅以 field-开头的输入字段的列表,那么我们要创建一个名为 $ mapped_fields 的关联数组,该数组以提取的字段名称作为键,而实际输入字段的值作为值。

what we want to do is, first, get a list of only those input fields who actually start with "field-", then we want to create an associative array called $mapped_fields that has the extracted field name as the key and the actual input field's value as the value.

$mapped_fields = array_reduce( preg_grep( '/field-.+/', array_keys( $_POST ) ), function( $hash, $field ){ $hash[substr( $field, 6 )] = $_POST[$field]; return $hash; } );

哪些输出:

Array ( [date_of_birth] => 1 [user_email] => 2 [last_name] => 3 [first_name] => 4 [current_position] => 6 )

(所以,为了阻止反对者,让我同意,这段紧凑的代码可以说比一个简单的代码更难读循环遍历$ _POST的循环,并检查每个键是否具有前缀,如果有,则将其及其值弹出到数组中)

(So, just to forestall the naysayers, let me agree that this bit of compact code is arguably a lot less readable that a simple loop that iterates through $_POST and, for each key, checks to see if it has the prefix, and if so, pops it and its value onto an array)

推荐答案

几天前,我遇到了完全相同的问题。无法使用 array_map ,但是 array_reduce 可以解决问题。

I had the exact same problem some days ago. It is not possible using array_map, but array_reduce does the trick.

$arr = array('a','b','c','d');
$assoc_arr = array_reduce($arr, function ($result, $item) {
    $result[$item] = (($item == 'a') || ($item == 'c')) ? 'yes' : 'no';
    return $result;
}, array());
var_dump($assoc_arr);

结果:

array(4){[ a] => string(3)是 [ b] => string(2) no [ c] => string(3)是 [ d] => string(2) no}

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

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