0

这是我的数组

Array
(
    [0] => Array
        (
            [id] => 277558
            [text_value] => Jif
            [response_count] => 13
            [response_percentage] => 92
        )
    [1] => Array
        (
            [id] => 277559
            [text_value] => Peter Pan
            [response_count] => 20
            [response_percentage] => 6
        )
)

完成操作后的输出应该是

Array
(
    [0] => Array
        (
            [id] => 277558
            [text_value] => Jif
            [response_count] => 13
            [response_percentage] => 92
            [encode_param]=>ds!@@^(*!ggsfh8236542jsdgf82*&61327
        )
    [1] => Array
            (
                [id] => 277559
                [text_value] => Peter Pan
                [response_count] => 20
                [response_percentage] => 6
                [encode_param]=>ds!@@^(*!ggsfh8236542jsdgf82*&61327
            )
)

你可以看到一个新的数组值 encode_paramis 添加

在那个函数中做一些编码算法

我已经在 foreach 循环语句中实现了这一点,但我需要在数组映射中做到这一点

谁能帮忙提前谢谢你

4

1 回答 1

1
$encode_func = function($elem) {   // declare function to encode
  return $elem['text_value']; 
}

$result = array_map(function($elem) use($encode_func) { 
  $elem['encode_param'] = $encode_func($elem);
  return $elem;
}, $array);

希望能帮助到你。

于 2015-01-08T08:33:56.160 回答