here is the code instead of the thousand code

var_dump($model-image); 

getting

 object(common\models\Image)#2028 (9) { ["class"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(13) { ["title_alt"]=> string(46) "Ложное обвинение (сериал)" ["path"]=> string(15) "serial/2016/10/" ["name"]=> string(10) "979247.jpg" ["for_home"]=> int(1) } } 

then do so

 $img = array_filter($model->image, function($item) { return $item->for_home == 1; }); var_dump($img) 

am getting null why am i not getting an object? in $img

  • It is strange that this code does not give you an error. $ model-> image is traversed as an array of attributes. Accordingly, $ item in the function is either an int or a string, but not an object that could be used to access the -> for_home - cronfy property
  • @cronfy is not a complete object its part for readability you all publish? - Sergalas
  • completely not necessarily, but somehow show what you have in model-> image? - an object, an array of objects or something else. - cronfy
  • In principle, the question decided there Yii2 troubles was thanks for your attention :) - Sergalas

1 answer 1

Well, probably the function for arrays does not apply to the object.

If you want to filter attributes, you should still pass them to the function:

array_filter($model->image->attributes, ...

attributes - returns an array of attributes and their values

But it is not clear what you expect as a result, you can simply get the value of the for_home property:

$model->image->for_home

or

$model->image->getAttributes('for_home')

  • This function works with objects already tested. I do not need to filter, but I need to choose from 5 or 6 objects of the kind that I showed an object with the property for_home = 1 - Sergalas
  • you probably mean that it works with an array of objects, in your question you show that you are trying to apply it to an object - Bookin