0

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'));
7
  • Seems to work with typos ($column_name vs $colum_name, array_collumns vs. array_column) corrected. 3v4l.org/B7qaT Commented Nov 21, 2018 at 21:01
  • despite the typos, the problem stays the same. In my real code it's without typos. Commented Nov 21, 2018 at 21:06
  • Can you post a reproducible example? As can be seen in demo they both work the same. If array_collumn is not the native array_column function can you please add that definition? 3v4l.org/EuOfb Commented Nov 21, 2018 at 21:09
  • 2
    Ucwords is not the same as strtoupper. Read the manual what ucwords does Commented Nov 21, 2018 at 21:21
  • 1
    oh my. Of course. Thanks mate! Commented Nov 21, 2018 at 21:23

1 Answer 1

2

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

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

https://3v4l.org/c6cum

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.