Voting

: nine minus eight?
(Example: nine)

The Note You're Voting On

katrinaelaine6 at gmail dot com
8 years ago
array_column() will return duplicate values.

Instead of having to use array_unique(), use the $index_key as a hack.

**Caution: This may get messy when setting the $column_key and/or $index_key as integers.**

<?php

$records
= [
[
'id' => 2135, 'first_name' => 'John' ],
[
'id' => 3245, 'first_name' => 'Sally' ],
[
'id' => 5342, 'first_name' => 'Jane' ],
[
'id' => 5623, 'first_name' => 'Peter' ],
[
'id' => 6982, 'first_name' => 'Sally' ]
];

print_r(array_unique(array_column($records, 'first_name')));

// Force uniqueness by making the key the value.
print_r(array_column($records, 'first_name', 'first_name'));
print_r(array_column($records, 'id', 'first_name'));

// Returns
/*

Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)

Array
(
[John] => John
[Sally] => Sally
[Jane] => Jane
[Peter] => Peter
)

Array
(
[John] => 2135
[Sally] => 6982
[Jane] => 5342
[Peter] => 5623
)

*/

?>

<< Back to user notes page

To Top