-2

I'm trying to filter array from DB and I've got this postman response:

{
"1": {
    "id": "3",
    "key": "emails_html_body_start",
    "value": "value"
}}

How I can access to id, key, value?
My code here:

$start = array_filter($array, function ($var) {
    return ($var['key'] == 'emails_html_body_start');
}); 
echo json_encode($start);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    [**Please Never** post images of or off site links to Code, Data or Error Messages](https://meta.stackoverflow.com/a/285557/2310830). Please edit your question and include copy/paste the text into the question, formatted. This is so that we can try to reproduce the problem without having to re-type everything, and your question can be properly indexed or read by screen readers. – RiggsFolly Feb 06 '23 at 16:20
  • https://stackoverflow.com/questions/804850/get-php-class-property-by-string – Filip Vnenčák Feb 06 '23 at 16:46
  • `array_filter()` returns an array. Use `var_dump()` to print it, not `echo`. – Barmar Feb 06 '23 at 16:50
  • Your question is a bit unclear ... So the upper code is what is sent by the lower code snippet? So the `cho json_encode($start);` is what produces the upper json data? If so, then you obviously need to json decode that data again to be able to access a property inside that structure. – arkascha Feb 06 '23 at 16:54
  • It would be useful to have the input an expected output. As far as I can tell this does exactly what you coded it to do, Given a multidimensional array with many elements like this (id, key, value), I would expect exactly what you got based on the code I see. Everything except what is in your JSON (output) would be removed because `$var['key'] == 'emails_html_body_start'` would apparently be false. Therefore the error must be in your logic ( what you expect ), which is a mystery at this point. – ArtisticPhoenix Feb 06 '23 at 17:25

1 Answers1

-1

Your question is a bit unclear ... So the upper code is what is sent by the lower code snippet? So the cho json_encode($start); is what produces the upper json data?

If so, then you obviously need to json decode that data again to be able to access a property inside that structure:

<?php
$input = <<<JSON
{
  "1": {
    "id": "3",
    "key": "emails_html_body_start",
    "value": "value"
  }
}
JSON;
$data = json_decode($input, true);
$output = $data[1]['id'];
print_r($output);

The output obviously is:

3

arkascha
  • 41,620
  • 7
  • 58
  • 90