[英]how can i remove array from STDObject?
Hello!! 你好!!
i think its very simple question for those who familier with std objects and arrays in PHP. 对于那些熟悉PHP中的std对象和数组的人来说,我认为这是一个非常简单的问题。
Here is my STD Object array: 这是我的STD对象数组:
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 . 我需要检查这些数字中的一个是否等于[category_id]值(在std对象内),如果相等,检查下一个[category_id],如果不相等,则删除该对象的所有值。 (of course, Other method is to build a new STD Object only with the numbers in the array)
(当然,其他方法是仅使用数组中的数字构建新的STD对象)
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.. 只有带有categiry_id = 17和10的数组才在此stdObject中。
Thank you very much for your help!! 非常感谢您的帮助!!
Eran. 伊兰。
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
You can use array_filter
to filter what you want or what you don't want 您可以使用
array_filter
来过滤您想要的或您不想要的内容
$filterCategory = array(17,10);
$array = array_filter($yourArray,function($var) use($filterCategory) { return in_array($var->category_id, $filterCategory); } );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.