Home > Software design >  PHP How can I find the key of an array element with name=>subject?
PHP How can I find the key of an array element with name=>subject?

Time:09-16

How can I find the key of an array element with name=>subject? For example in the attached picture I should get the result 20

As you can see, I'm using messages with gmail api, I want to get the subject of a message, but the subject of each message has a different key. –

Array: enter image description here

CodePudding user response:

Surely it's as simple as this...

$subjects = array_filter($headers, fn($obj) => $obj->name === 'Subject');

Replacing the $headers variable with your array.


EDIT

Misread the question, OP is trying the get the key of the array not the value (which the above does).

You could do something like...

$subject_key = array_search('Subject', array_map(fn($obj) => $obj->name, $headers))
  •  Tags:  
  • php
  • Related