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

Ivan
  • 2,463
  • 6
  • 39
  • 51
  • please state your current attempt. also `array_column()` should help – Rotimi Mar 14 '18 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.. – Kaddath Mar 14 '18 at 12:30
  • There are plenty of solutions in that duplicate that don't involve `array_column`… – deceze Mar 14 '18 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 :-) ) – Ivan Mar 14 '18 at 12:48
  • Fine, more options here: https://stackoverflow.com/a/43120997/476, https://stackoverflow.com/a/31590256/476 – deceze Mar 14 '18 at 13:05

0 Answers0