T
T
ts-lexx2015-10-06 21:25:31
PHP
ts-lexx, 2015-10-06 21:25:31

How to read data from json using php?

Good time of the day!
Tell me, who knows how to correctly read certain data from a json file!?
The file has the following structure:

{
  "site": [
    {
      "tab": {
        "lookup": "primer_pole",
        "label": "primer"
      },
      "settings": [
        {
          "lookup": "qw_qw",
          "label": "qwqw",
          "type": "text",
          "access": "normal"
        }
      ]
    },
    {
      "tab": {
        "lookup": "mail-form",
        "label": "mail-form"
      },
      "settings": [
        {
          "lookup": "email",
          "label": "email",
          "type": "text",
          "access": "normal",
          "value": "[email protected]"
        }
      ]
    }
  ]
}

it is necessary to get the "value" value exactly from the "lookup": "mail-form"
$json = file_get_contents('url/data.json');
$UserData = json_decode($json, true);
and then I don't know how. ((
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Defman21, 2015-10-06
@Defman21

$UserData['site'][1]['settings']['value']

M
Mykola, 2015-10-06
@iSensetivity

<?php

$str = '{
   "site":[
      {
         "tab":{
            "lookup":"primer_pole",
            "label":"primer"
         },
         "settings":[
            {
               "lookup":"qw_qw",
               "label":"qwqw",
               "type":"text",
               "access":"normal"
            }
         ]
      },
      {
         "tab":{
            "lookup":"mail-form",
            "label":"mail-form"
         },
         "settings":[
            {
               "lookup":"email",
               "label":"email",
               "type":"text",
               "access":"normal",
               "value":"[email protected]"
            }
         ]
      }
   ]
}';

$json = json_decode($str, true);
$email = $json['site'][1]['settings'][0]['value'];

T
ts-lexx, 2015-10-06
@ts-lexx

Guys, thank you! Helped out.
If it's not difficult, tell me about the sampling cycle:

if ($i = array_search($UserData, "mail-form") !== false)
$Mailaddress = $UserData["site"][$i]["settings"][0]["value"];

I guess I'm doing the wrong search!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question