-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?

6
  • Does this answer your question? Yii2 - ActiveRecord to Array Commented May 5, 2020 at 9:35
  • Not really, this is refering to ActiveRecord not ActiveQuery. Any other suggestions? Commented May 5, 2020 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. yiiframework.com/doc/api/2.0/yii-db-activequery Commented May 5, 2020 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()? Commented May 5, 2020 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? Commented May 5, 2020 at 10:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

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.