-1

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

  • Please [edit] your post when you want to add something, rather than posting a comment (they are just for people to ask for clarification or give brief suggestions). See also [ask] and the [tour]. Thanks. – ADyson Sep 15 '22 at 13:19
  • And as per that [How To Ask](https://stackoverflow.com/help/how-to-ask) guide, which you are encouraged to read before using the site, please don't post images of your code. Code is text. Pasting it as graphics is very impractical as it can't be copied, searched, re-used in answers etc. It makes it difficult for those who might want to help you. Please [edit] your question to include the code as text and use the [formatting tools](https://stackoverflow.com/help/formatting) to present it nicely, so that it is usable for those who want to help you. Thanks – ADyson Sep 15 '22 at 13:20
  • It's also not clear where this output comes from either...is it the result of some specific command? It doesn't look like it comes from any of the usual PHP debugging commands such as var_dump etc. – ADyson Sep 15 '22 at 13:20
  • This is the output of a dd() function, not code. – Haktan Aydın Sep 15 '22 at 13:24
  • 1
    I should probably have said "don't post images of your code **or data**". It results in the same practical problem for the people reading your question, as is surely obvious to you from what I wrote above. – ADyson Sep 15 '22 at 13:27
  • 1
    Anyway if you're trying to get things from a PHP array, then possibly [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) will help you, or maybe if you're searching for things then [PHP multidimensional array search by value](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) would be more useful. Either way I doubt this is a new or unusual problem. There are other similar resources available too...it's unclear what research you did before asking this question, or what attempt(s) you made? – ADyson Sep 15 '22 at 13:30
  • 1
    Use `foreach` loop for `array` then check `if($array["name"] == "Subject"){echo $array["value"];}` Also please add code/json/array instead of an image next time :) – c0de b0unty Sep 15 '22 at 13:43
  • @c0deb0unty foreach will slow down my site a lot because i am using 2 nested foreach right now however there will be 3rd foreach. $key = array_search('subject', array_column($array, 'name')); I found this on the internet but it returns me 'false' – Haktan Aydın Sep 15 '22 at 13:52

1 Answers1

0

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))
Levi Cole
  • 3,561
  • 1
  • 21
  • 36