dounielong7728 2012-10-13 10:26
浏览 30
已采纳

如何从STDObject中删除数组?

Hello!!

i think its very simple question for those who familier with std objects and arrays in PHP.

Here is my STD Object array:

Array ( [0] => stdClass Object ( [name] => name1 [description] => [category_id] => 17 [category_publish] => 1 [ordering] => 1 [category_parent_id] => 10 ) [1] => stdClass Object ( [name] => name2 [description] => [category_id] => 8 [category_publish] => 1 [ordering] => 1 [category_parent_id] => 0 ) [2] => stdClass Object ( [name] => name3 [description] =>desc [category_id] => 10 [category_publish] => 1 [ordering] => 2 [category_parent_id] => 0 ) [3] => stdClass Object ( [name] => name3 [description] => [category_id] => 16 [category_publish] => 1 [ordering] => 2 [category_parent_id] => 10 ) )

now, i have different array with numbers for example:

$arr=array(17,10);

and i need to check if one of this numbers is equal the [category_id] value (inside the std object), if it is equal, check the next [category_id], and if it's not, remove all values of this object . (of course, Other method is to build a new STD Object only with the numbers in the array)

So the result should be:

Array ( [0] => stdClass Object ( [name] => name1 [description] => [category_id] => 17 [category_publish] => 1 [ordering] => 1 [category_parent_id] => 10 ) [2] => stdClass Object ( [name] => name3 [description] =>desc [category_id] => 10 [category_publish] => 1 [ordering] => 2 [category_parent_id] => 0 ) )

Only the array's with categiry_id =17 and 10 are inside this stdObject..

Thank you very much for your help!!

Eran.

  • 写回答

2条回答 默认 最新

  • doulu1945 2012-10-13 10:29
    关注

    Try this

    $arr=array(17,10);
    foreach ($array as $key => $obj) {
      if (!in_array($obj->category_id, $arr)) {
        unset($array[$key]);
      }
    } 
    // edited, missing closing bracket
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统