0

I have an array of objects and I want to convert it to an array of the result of a method of each of them. I can do this just fine, but I'm wondering if there is a cleaner / better approach to it maybe? For example, pretend this is what I'm working with and how I'm doing it now:

$objects = array();

$objects[] = new Dog();
$objects[] = new Dog();
$objects[] = new Dog();

$data = array();
foreach ($objects as $obj) {
  $data[] = $obj->myMethod();
}

Obviously this isn't super important, but it'd be nice to know about better ways to produce $data from $objects in the future. Any ideas? I was thinking there was some function for this, like array_map() or something but I'm not finding it.

4 Answers 4

1

you could indeed use PHP's array_map() to do this

function cb($obj) { return $obj->myMethod(); }
.
.
$data = array_map(cb, $objects);
Sign up to request clarification or add additional context in comments.

3 Comments

In php 5.3 you can use anonymous functions instead of function cb($obj)
ah yeah 5.3 - the version that doesn't run properly in CLI mode for me on Windows 7 :( - but indeed that'd be a better way to do it...
This does look like the best way to go about it - thanks. And yeah I like the 5.3 version also but don't have it locally or on the server this is heading to.
1

If you have php 5.3 or better, the neater way is to use array_map() with anonymous functions:

$retArray = array_map(function($o){ $o->myMethod(); }, $myArray);

If you don't have php 5.3, you are left with having to declare the function before hand and passing the function name to array_map()

Comments

1

You could use a bit more OOP:ish approach using iterators.

class TrainedDogIterator implements Iterator {
    // implement methods on http://php.net/iterator
    public function current() {
        $dog = current($this->dogs); // $this->dogs would be your objects
        $trained_dog = $this->_trainDog($dog);
        return $trained_dog;
    }
    private function _trainDog($dog) {
        // do something with dog
        return $dog;
    }
}

Use it where you would use $data in your example.

$di = new TrainedDogIterator($dogs);
foreach($di as $dog) {
    // $dog is trained
}

2 Comments

This is a good/different approach on the problem, so thanks - could come in handy for other things as well. In this case I'm using a reused function that returns the array of objects, which in other cases need to be as-is. Though, implementing this approach may put the functionality in a more appropriate place in the code.
@pssdbt: the code was kind of a long shot, glad you enjoyed it :) If you give a bigger image of your system, you'd get more feedback about the overall design but I agree that you marked the right answer as correct in this case.
0

TMTOWTDI, array_walk works similarly to array_map, but modifies the array in-place. Though array_map has the advantage of supporting multiple arrays:

https://www.php.net/array_walk

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.