PHP. array_column을 객체 배열과 함께 사용할 수 있습니까?
합격할 수 있을까요?array_column사물의 배열?
Array Access 인터페이스를 구현했지만 효과가 없습니다.
다른 것을 구현해야 합니까?
class Foo implements ArrayAccess {
public $Id, $Title;
public function offsetExists($offset)
{
return isset($this->{$offset});
}
public function offsetGet($offset)
{
return $this->{$offset};
}
public function offsetSet($offset, $value)
{
$this->{$offset} = $value;
}
public function offsetUnset($offset)
{
unset($this->{$offset});
}
}
$object = new \Foo();
$object->Id = 1;
$object->Title = 'Test';
$records = array(
$object,
array(
'Id' => 2,
'Title' => 'John'
)
);
var_dump(array_column($records, 'Title')); // array (size=1) 0 => string 'John' (length=4)
PHP 5
array_column오브젝트 배열에서는 동작하지 않습니다.사용하다array_map대신:
$titles = array_map(function($e) {
return is_object($e) ? $e->Title : $e['Title'];
}, $records);
PHP 7
array_column()
이 함수는 이제 2차원 배열뿐만 아니라 객체 배열도 지원합니다.퍼블릭 속성만 고려되며, 다음 속성을 사용하는 오브젝트만 고려됩니다.
__get()동적 속성에 대해서도 구현해야 합니다.__isset().
https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L629 - Thanks to Bell 힌트를 참고하세요!
array_column에 객체 배열을 전달할 수 있습니까?
PHP 7
네, http://php.net/manual/en/function.array-column.php 를 참조해 주세요.
PHP 5 > = 5.5.0
PHP 5의 경우array_column는 오브젝트 배열에서는 동작하지 않습니다.다음 방법으로 시험해 볼 수 있습니다.
// object 1
$a = new stdClass();
$a->my_string = 'ciao';
$a->my_number = 10;
// object 2
$b = new stdClass();
$b->my_string = 'ciao b';
$b->my_number = 100;
// array of objects
$arr_o = array($a,$b);
// using array_column with an array of objects
$result = array_column(array_map(function($o){return (array)$o;},$arr_o),'my_string');
PS: 알기 쉽게 하기 위해 사용하지 않는 것이 좋습니다.array_columnarray_map을 어나니머스 함수와 함께 사용합니다.
$result = array_map(function($o){ return $o->my_string; }, $arr_o);
또는 단순foreach
$result = array();
foreach($arr_o as $o) {
$result[] = $o->my_string;
}
php7과 php5에서 모두 동작하는 함수는 다음과 같습니다.
function array_column_portable($array, $key) {
return array_map(function($e) use ($key) {
return is_object($e) ? $e->$key : $e[$key];
}, $array);
}
그런 다음 php7에서 array_column을 사용할 수 있습니다.
가능한 해결책은 다음과 같은 객체 배열을 준비하는 것입니다.
$objectsList = [];
foreach ($objs as $obj) {
$objectsList[] = (array)$obj;
}
$propList = array_column($objectsList, 'prop');
사용할 수 없지만array_column자녀 양육에서는 어레이에 캐스팅할 수 있습니다.이것은 물론 갈 길은 아니지만 갈 길이다.
사용(고객의 경우 어레이가 포함되어 있기 때문에 작동하지 않음)
array_column(array_map('get_object_vars', $thingy), 'property');
사용.json_decode/json_encode
array_column(json_decode(json_encode($thingy), true), 'property');
주의: json을 사용하면 실제 재귀 함수를 사용하는 것과 같은 결과가 되지 않습니다.개체의 보호 및 개인 속성이 손실됩니다.하지만 어떤 상황에서는 괜찮다.
function object_to_array($object) {
if (is_object($object)) $object = get_object_vars($object);
return is_array($object) ? array_map(__FUNCTION__, $object) : $object;
}
언급URL : https://stackoverflow.com/questions/23335845/php-is-it-possible-to-use-array-column-with-an-array-of-objects
'programing' 카테고리의 다른 글
| 비 ASC와 일치하는 정규 표현II 캐릭터? (0) | 2022.11.30 |
|---|---|
| 참조별 JavaScript 대 값별 JavaScript (0) | 2022.11.30 |
| MySQL/MariaDB: 'GRANT...'의 차이점은 무엇입니까?'사용자 생성'과 '허가'로 구분됩니까? (0) | 2022.11.30 |
| VueJ가 어레이 길이 변경에 응답하지 않습니까? (0) | 2022.11.30 |
| Vue.js 엄밀한 모드에서는 허용되지 않는 속성의 다중 정의 (0) | 2022.11.30 |