在PHP中使用数组地图功能添加新的索引,而无需使用循环功能 [英] add new index using array map function in php without using looping function

查看:350
本文介绍了在PHP中使用数组地图功能添加新的索引,而无需使用循环功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组

 阵列

    [0] =>排列
        (
            [ID] => 277558
            [TEXT_VALUE] =>日夫
            [response_count] => 13
            [response_percentage] => 92
        )
    [1] =>排列
        (
            [ID] => 277559
            [TEXT_VALUE] =>彼得潘
            [response_count] => 20
            [response_percentage] => 6
        )

在完成操作了放应

 阵列

    [0] =>排列
        (
            [ID] => 277558
            [TEXT_VALUE] =>日夫
            [response_count] => 13
            [response_percentage] => 92
            [CN code_param] =>!DS @@ ^(* ggsfh8236542jsdgf82 *安培; 61327
        )
    [1] =>排列
            (
                [ID] => 277559
                [TEXT_VALUE] =>彼得潘
                [response_count] => 20
                [response_percentage] => 6
                [CN code_param] =>!DS @@ ^(* ggsfh8236542jsdgf82 *安培; 61327
            )

您可以看到EN code_paramis增加了新的数组值

在该功能做一些带code算法

我在foreach循环语句实现这一目标
但我需要做的是在阵列马平

人帮助可以感谢ü提前


解决方案

  $连接code_func =功能($ ELEM){//声明功能EN code
  返回$ ELEM ['TEXT_VALUE'];
}$结果= array_map(函数($ ELEM)使用($连接code_func){
  $ ELEM [恩code_param'] = $连接code_func($ ELEM);
  返回$ ELEM;
},$阵列);

希望它帮助。

this is my array

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
        )
)

after completing the operation the out put should be

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
            )
)

you can see a new array value encode_paramis added

in that function do some encode algorithms

i have achieve this in the foreach looping statement but i need to do it in array maping

Can anybody help thank u in advance

解决方案

$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);

Hope it helps.

这篇关于在PHP中使用数组地图功能添加新的索引,而无需使用循环功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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