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

2
  • Have you actually looked at array_map() or array_walk() at all? Commented Jan 8, 2015 at 8:28
  • ya i have done some search on it but i could not fine the solution Commented Jan 8, 2015 at 8:42

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

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

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 ) )
@DeepakKumar Are you sure you pass your array as last param in call to array_map()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.