PHP intersection between array and object
Posted
by nickf
on Stack Overflow
See other posts from Stack Overflow
or by nickf
Published on 2010-04-08T00:28:31Z
Indexed on
2010/04/08
0:33 UTC
Read the original article
Hit count: 468
I have an object, let's say it's like this:
class Foo {
var $b, $a, $r;
function __construct($B, $A, $R) {
$this->b = $B;
$this->a = $A;
$this->r = $R;
}
}
$f = new Foo(1, 2, 3);
I want to get an arbitrary slice of this object's properties as an array.
$desiredProperties = array('b', 'r');
$output = magicHere($foo, $desiredProperties);
print_r($output);
// array(
// "b" => 1,
// "r" => 3
// )
© Stack Overflow or respective owner