Find all JSON records by specific value

Viewed 66

I have a JSON array of data

Array example:

{
  "items": [
    {
      "ЮЛ": {
        "ИНН": "xxxxxxxxxxxxxxx",
        "ОГРН": "xxxxxxxxxxxxxxx",
        "НаимСокрЮЛ": "xxxxxxxx"",
        "НаимПолнЮЛ": "xxxxxxxxxxxxxxxxxxx"",
        "ДатаОГРН": "xxxxxxxx",
        "Статус": "xxxxxxxx",
      }
    },
    {
      "ЮЛ": {
        "ИНН": "xxxxxxxxxxxxxxx",
        "ОГРН": "xxxxxxxxxxxxxxx",
        "НаимСокрЮЛ": "xxxxxxxx"",
        "НаимПолнЮЛ": "xxxxxxxxxxxxxxxxxxx"",
        "ДатаОГРН": "xxxxxxxx",
        "Статус": "xxxxxxxx",
      }
    },
    {
      "ЮЛ": {
        "ИНН": "xxxxxxxxxxxxxxx",
        "ОГРН": "xxxxxxxxxxxxxxx",
        "НаимСокрЮЛ": "xxxxxxxx"",
        "НаимПолнЮЛ": "xxxxxxxxxxxxxxxxxxx"",
        "ДатаОГРН": "xxxxxxxx",
        "Статус": "xxxxxxxx",
      }
    },

I need to display all the records found, only for a specific ИНН. How can i do this?

My attempt looks like this (does not work):

    $inn = '7203186956';
    echo "<table class='table table-bordered'><h3>Table</h3>";

    foreach (json_decode($data, true)['items'] as $key => $val) {
        if ($val['ЮЛ']['ИНН'] == $inn) {
            echo "<th scope='col'>$key</th><td colspan='3'>$val</td>";
        }
    }
    echo "</table>";

My attempt looks like this (work code):

    $inn = '7203186956';
    echo "<table class='table table-bordered'><h3>Table</h3>";
    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {
            echo "<tr><th scope='row'>$key</th></tr>";
        } else {
            echo "<th scope='col'>$key</th><td colspan='3'>$val</td>";
        }
    }
    echo "</table>";

thank you all in advance!

0 Answers
Related