-1

Feels like it should be easy...

function flattenTags($t) {
  return ($t['name']);
}
$tags = array($Fan->getTags());
$flat_tags = array_map('self::flattenTags', $tags);
$string_tags = join(', ', $flat_tags);

where tags is an array of objects each with multiple properties.

Error:

Cannot use object of type yii\\db\\ActiveQuery as array

In JS this is a simple array.map().join(). Can't get it to work with PHP. Please help!

EDIT: do I need some kind of await like JS?

chrisheseltine
  • 193
  • 2
  • 13
  • Does this answer your question? [Yii2 - ActiveRecord to Array](https://stackoverflow.com/questions/31125334/yii2-activerecord-to-array) – CBroe May 05 '20 at 09:35
  • Not really, this is refering to ActiveRecord not ActiveQuery. Any other suggestions? – chrisheseltine May 05 '20 at 10:12
  • ActiveQuery has the same method, as the person asking the question there states right at the beginning … And you can easily go read up on it in the documentation, when in doubt. https://www.yiiframework.com/doc/api/2.0/yii-db-activequery – CBroe May 05 '20 at 10:15
  • Apolagies I'm brand new to PHP and Yii so I'm struggling a little, I don't have time to fully learn them from the docs. Is the line you're referring to `asArray()`? – chrisheseltine May 05 '20 at 10:27
  • When I try that I get `Call to a member function asArray() on array`. So I cannot call `asArray()` because it is already an array, but when passing it to map it's not an array. Any ideas? – chrisheseltine May 05 '20 at 10:34
  • Show _what exactly_ you tried, instead of paraphrasing it. – CBroe May 05 '20 at 10:35

1 Answers1

0

I figured it out:

function flattenTags($t) {
  return ($t['name']);
}
$tags = $Fan->getTags()->all();
$flat_tags = array_map('self::flattenTags', $tags);
$string_tags = join(', ', $flat_tags);

The missing part was the all() command which converts a query into an array.

chrisheseltine
  • 193
  • 2
  • 13