3

I have a array $services and this array contains below values :

Array
(
  [0] => One way
  [1] =>  Hourly
  [2] =>  To Airport
  [3] =>  From Airport
  [4] =>   Birthday
  [5] =>  Wedding
  [6] =>  Concert
  [7] =>   Sporting Event
  [8] =>   Cruise Party
  [9] =>  Funeral
)

You can notice that some of values in array contain space.
To remove this space from array values I created a array_walk function which walk into array and trim the white space.

public function trim_value(&$value) {
     $value = trim($value);
}  

As you all know that the syntax of array_walk function is :

array_walk($array, 'callback_function');

Now I want to use this function in codeigniter controller. As I know that a function in controller is being used as $this->function in another function.
So I tried to use callback function as :

array_walk($services, $this->trim_value);

It always throws an below error :

A PHP Error was encountered

<p>Severity: Notice</p>
<p>Message:  Undefined property: Attribute::$trim_value</p>
<p>Filename: controllers/attribute.php</p>
<p>Line Number: 230</p>

So can somebody tell me that how to use callback function in one of controller's function ?

1
  • No, I don't agree with you because there are many inbuilt function in php library that are used with callback function. Commented Apr 14, 2015 at 6:00

1 Answer 1

9

You have to pass array_walk() an array with $this as a defined variable/pointer.

Try this

array_walk($array, array($this, 'trim_value'));
Sign up to request clarification or add additional context in comments.

1 Comment

Beat me to it - I just found that in php.net/manual/en/function.array-walk.php and tried it out - It works!

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.