s_h
s_h

Reputation: 1496

Return 2 or more values using map(function ($data) in PHP

I cannot find the way. Im using the following Algolia option to create a relationship.

public function toSearchableArray()
{
    $array = $this->toArray();

    $array['categories'] = $this->categories->map(function ($data) {
                             return $data['name'];
                           })->toArray();

   return $array;
}

Despite I read php page for array_map I cannot find the way to return more than one value, as an example:

function ($data) {{return $data['name']; return $data['value'] ;})->toArray(),

gives no error but no output too.

Upvotes: 1

Views: 941

Answers (1)

mega6382
mega6382

Reputation: 9396

Try the following:

$array['categories'] = $this->categories->map(function ($data) {
                         return [$data['name'], $data['value']];
                       })->toArray();

You can return an array instead of trying to return 2 strings.

Upvotes: 2

Related Questions