1

Given this array:

$columns = [
    ['data' => 'id', 'name' => 'id', 'title' => 'ID', 'class' => 'text-center text-shrink', 'orderable' => false, 'searchable' => false],
    ['data' => 'name', 'name' => 'name', 'title' => 'Nome', 'default' => true],
    ['data' => 'email', 'name' => 'email', 'title' => 'E-mail'],
    ['data' => 'created_at', 'name' => 'created_at', 'title' => 'Creato il', 'class' => 'text-center'],
    ['data' => 'updated_at', 'name' => 'updated_at', 'title' => 'Aggiornato il', 'class' => 'text-center'],
    ['data' => 'action', 'name' => 'action', 'title' => 'Azioni', 'class' => 'text-center text-shrink', 'orderable' => false, 'searchable' => false],
];

How do I retrieve the key of the only item that has 'default' => true (it should be 1 in this case)? I could do it with a loop, but isn't there an easy way for trivial task like this? Please notice that I can also use Laravel helpers

Please notice that this is not a duplicate of a previous question: here I am sure that the default subkey exists in only one array item and not in the others, so I can't use array_column() and then get the one with a certain value

Thank you

5
  • please state your current attempt. also array_column() should help Commented Mar 14, 2018 at 12:28
  • 2
    loops are usually the most efficient way for this kind of tasks, i always wonder why so many people always prefer less readable and performant solutions.. Commented Mar 14, 2018 at 12:30
  • There are plenty of solutions in that duplicate that don't involve array_column Commented Mar 14, 2018 at 12:43
  • @deceze, the other solutions involve a loop: I stated in my question that I could go with it but I'm looking for a "two lines solution" (if any, of course :-) ) Commented Mar 14, 2018 at 12:48
  • Fine, more options here: stackoverflow.com/a/43120997/476, stackoverflow.com/a/31590256/476 Commented Mar 14, 2018 at 13:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.