Search code examples
phparraysarray-merge

How to merge data returned from multiple method calls?


I created this working code using array_merge in php.

getData() is returning an array.

    $cars = array();
    $cars = array_merge($cars, $this->getData($x, $y, $z, 0, -1));
    $cars = array_merge($cars, $this->getData($x, $y, $z, 1, -1));
    $cars = array_merge($cars, $this->getData($x, $y, $z, 1, 0));
    $cars = array_merge($cars, $this->getData($x, $y, $z, 1, 1));

There is a better way to do the same?


Solution

  • array_merge() accepts multiple arguments:

    $cars = array_merge($this->getData($x, $y, $z, 0, -1),
                        $this->getData($x, $y, $z, 1, -1),
                        $this->getData($x, $y, $z, 1, 0),
                        $this->getData($x, $y, $z, 1, 1));