2

I have an array containing single key objects like so:

Array
(
    [0] => stdClass Object
        (
            [state] => 1
        )

    [1] => stdClass Object
        (
            [state] => 1
        )

)

I want it to look like this:

Array
(
    [0] => 1

    [1] => 1

)

What is the most efficient way of doing this? I'm not quite sure how to put this problem in simple words, so I can't google it either.

4

4 回答 4

3

You could use array_map:

$result = array_map(function($object) {
    return $object->state;
}, $originalArray);
于 2013-07-19T14:37:10.757 回答
1

you can do it with a for loop :

for $array in $val
   $val =$val[state]
于 2013-07-19T14:37:04.357 回答
0

You can use array_walk and pass the value in by reference:

array_walk($array, function(&$v, $i) { 
    $v = $v->state;
});

or

array_walk($array, create_function('&$v', '$v = $v->state;'));
于 2013-07-19T14:38:06.127 回答
-1

If you got one of the newer PHP versions you can do that with a foreach loop and a reference:

foreach ($array as &$value)
{
  $value = $value->state;
}
于 2013-07-19T14:37:53.950 回答