php-src/ext/standard/tests/array/array_column_object_cast.phpt
Nikita Popov 091d53c131 Use standard key behavior in array_column()
array_column() reimplements array key handling in a way that does
not match standard array key behavior in PHP. Avoid this by making
use of the standard API.

Of course, there is a minor backwards compatibilty break here,
e.g. people could be relying on objects getting cast to string
instead of throwing.

Closes GH-5487.
2020-05-11 14:16:24 +02:00

47 lines
846 B
PHP

--TEST--
Test array_column() function: basic functionality
--FILE--
<?php
class ColumnKeyClass {
function __toString() { return 'first_name'; }
}
class IndexKeyClass {
function __toString() { return 'id'; }
}
$column_key = new ColumnKeyClass();
$index_key = new IndexKeyClass();
// Array representing a possible record set returned from a database
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'XXX'
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith'
),
);
$firstNames = array_column($records, $column_key, $index_key);
print_r($firstNames);
var_dump($column_key);
var_dump($index_key);
?>
--EXPECT--
Array
(
[2135] => John
[3245] => Sally
)
object(ColumnKeyClass)#1 (0) {
}
object(IndexKeyClass)#2 (0) {
}