There are objects they are stored in $ lt-> images; if so var_dump($lt->images); getting like that

 [0]=> object(Image) { ["id"]=> int(15) ["for_home"]=> int(1) } [1]=> object(Image){ ["id"]=> int(1693) ["for_home"]=> NULL } [2]=> object(Image) { ["id"]=> int(1694) ["for_home"]=> NULL } [3]=> object(Image) { ["id"]=> int(1695) ["for_home"]=> NULL } [4]=> object(Image){ ["id"]=> int(1696) ["for_home"]=> NULL } 

I'm trying to get an object that has for_home = 1 so

 foreach($lt->images as $image); if($image->for_home==1) { echo('yes'); } 

and of course I don’t get anything explain how to get an object from which for_home = 1

  • Try this: $image['for_home']==1 - Artem Y
  • with the design everything is in order - etki
  • Any errors are displayed or just the script does not display anything? - cheops
  • $lt->images it possible to $lt->images in question? And why ); after foreach? - Mr. Black
  • Pay attention to the register - Artem Y

2 answers 2

 <?php // для примера через json_encode/decode приобразовываем массив в объект $object = json_decode(json_encode(array( 'images' => array( array('id' => 15, 'for_home' => 1), array('id' => 1693, 'for_home' => NULL), array('id' => 1694, 'for_home' => NULL), array('id' => 1695, 'for_home' => NULL), array('id' => 1696, 'for_home' => NULL), )) ), FALSE); $for_home = array_filter($object->images, function($item) { return $item->for_home !== NULL; }); print_r($for_home); 

Result:

 Array ( [0] => stdClass Object ( [id] => 15 [for_home] => 1 ) ) 

    foreach for some reason foreach($lt->images as $image); starts with ; instead of {

     $lt = (object) array('images' => array( (object) array('id' => 15, 'for_home' => 1), (object) array('id' => 1693, 'for_home' => NULL), (object) array('id' => 1694, 'for_home' => NULL), (object) array('id' => 1695, 'for_home' => NULL), (object) array('id' => 1696, 'for_home' => NULL), )); foreach($lt->images as $image) { if($image->for_home == 1) { echo('yes'); } } 
    • Yes, this is like an example of non-compliance, of any coding style, so let's say in zend coding style it is recommended to always and everywhere set {} ... - Naumov
    • I don’t know why the author did the object(Image) , waiting for the experts - Mr. Black
    • I think he has some sort of implementation of Image - Naumov objects collection
    • one
      @Naumov; From the very beginning, he had some kind of implementation and he followed it - Mr. Black
    • one
      @Sergalas at least simply copy and paste replacing links and passwords with sample, etc. Just one typo and people have thought that the mistake is in it. And not there somewhere in the code from you. - Naumov