PHP array_filtered acces object

48 Views Asked by At

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);
1

There are 1 best solutions below

0
arkascha On

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