1

I have a quite simple question, yet I cannot get a way around it. I'd like to walk through an array and change it's items with a function, then return a new array of the new values.

Here is my array:

$array = array('thing1', 'thing2', 'thing3');

This is my function:

function testing ($string, $to)
{
    retrun substr($string, 0, $to);
}

And I'd like to do the following:

array_map(testing($string, 2), $array);

The above statement is not working properly. Can anyone tell me how to make this array_map function to work as expected?

1
  • See the documentation for array_map(). Pay attention to the examples. Also, you need to explain exactly what you mean by "not working properly" -- if we are to answer your question then we need to have something to go on. Commented Mar 7, 2014 at 22:09

1 Answer 1

3

Pass array_map an anonymous function that calls testing.

array_map(function($string){
    return testing($string, 2);
}, $array);
Sign up to request clarification or add additional context in comments.

Comments

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.