0

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

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Have you actually looked at [array_map()](http://php.net/manual/en/function.array-map.php) or [array_walk()](http://php.net/manual/en/function.array-walk.php) at all? – Mark Baker Jan 08 '15 at 08:28
  • ya i have done some search on it but i could not fine the solution – Deepak Kumar Jan 08 '15 at 08:42

1 Answers1

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

Hope it helps.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • it doesn't provide me the desire array the out put is like thisArray ( [text_value] => Array ( [encode_param] => c9f0f895fb98ab9159f51fd0297e236d ) [text_value1] => Array ( [encode_param] => c4ca4238a0b923820dcc509a6f75849b ) ) – Deepak Kumar Jan 08 '15 at 10:17
  • @DeepakKumar Are you sure you pass **your** array as last param in call to `array_map()`? – Aleksei Matiushkin Jan 08 '15 at 10:33