我想知道如何过滤数组对象中的值...
我只是显示下面是我的对象数组的一个数据
Object ( [_fields:private] => Array ( [a] => c7b920f57e553df2bb68272f61570210 [index_date] => 2010/05/11 12:00:58 [b] => i am zahir [c] => google.com [d] => 23c4a1f90fb577a006bdef4c718f5cc2 ) )
Object ( [_fields:private] => Array ( [a] => c7b920f57e553df2bb68272f61570210 [index_date] => 2010/05/11 12:00:58 [b] => i am zahir [c] => yahoo.com [d] => 23c4a1f90fb577a006bdef4c718f5cc2 ) )
Object ( [_fields:private] => Array ( [a] => c7b920f57e553df2bb68272f61570210 [index_date] => 2010/05/11 12:00:58 [b] => i am beni [c] => google.com [d] => 23c4a1f90fb577a006bdef4c718f5cc2 ) )
.
.
.
Object ( [_fields:private] => Array ( [a] => c7b920f57e553df2bb68272f61570210 [index_date] => 2010/05/11 12:00:58 [b] => i am sani [c] => yahoo.com [d] => 23c4a1f90fb577a006bdef4c718f5cc2 ) )
我必须过滤[c]值......
答案 0 :(得分:0)
不是我建议你到处访问私人领域(除了可能是属性注入),但你可以这样做:
class A {
private $variab = array();
public function __construct($val) {
$this->variab["c"] = $val;
}
}
$objects = array();
$objects[] = new A("value 1");
$objects[] = new A("value 2");
$objects[] = new A("value 3");
var_dump($objects);
$prop = new ReflectionProperty("A", "variab");
$prop->setAccessible(true);
$objects_filtered = array_filter($objects,
function (A $obj) use ($prop) {
$propval = $prop->getValue($obj);
return $propval["c"] != "value 2";
}
);
var_dump($objects_filtered);
$prop->setAccessible(false);
这给出了:
array(3) {
[0]=>
object(A)#1 (1) {
["variab":"A":private]=>
array(1) {
["c"]=>
string(7) "value 1"
}
}
[1]=>
object(A)#2 (1) {
["variab":"A":private]=>
array(1) {
["c"]=>
string(7) "value 2"
}
}
[2]=>
object(A)#3 (1) {
["variab":"A":private]=>
array(1) {
["c"]=>
string(7) "value 3"
}
}
}
array(2) {
[0]=>
object(A)#1 (1) {
["variab":"A":private]=>
array(1) {
["c"]=>
string(7) "value 1"
}
}
[2]=>
object(A)#3 (1) {
["variab":"A":private]=>
array(1) {
["c"]=>
string(7) "value 3"
}
}
}
编辑:由于您没有使用PHP 5.3.x,请尝试使用:
function filterFunc (A $obj) {
global $prop;
$propval = $prop->getValue($obj);
return $propval["c"] != "value 2";
}
$objects_filtered = array_filter($objects, "filterFunc");