Jake
Jake

Reputation: 185

Can't use variable in array_columns as column name

I have a simple array and I want to use array_column but strangely it does not work using a variable as column name.

$colors = array(
array( 'RAL' => 'RAL 1000', 'RGB' => '190,189,127', 'HEX' => 'BEBD7F', 
'NAME' => 'Grünbeige' ),
    array( 'RAL' => 'RAL 1001', 'RGB' => '194,176,120', 'HEX' => 'C2B078', 
'NAME' => 'Beige' ),
    );

$column_name = 'hex'; // this comes actually via $_GET['hex'];

This does not work:

print_r(array_column($colors, ucwords($column_name)));

This does work:

print_r(array_column($colors, 'HEX'));

Upvotes: 0

Views: 128

Answers (1)

Andreas
Andreas

Reputation: 23958

Don't use ucwords. That camelcase the word to Hex.
Use strtoupper.

print_r(array_column($colors, strtoupper($column_name)));

https://3v4l.org/c6cum

Upvotes: 2

Related Questions