-2

In PHP, I have learned that to be able to get values from an object is to do something like this:

$objResult->{"RESP"}->{"DATA"}->{"F_NAME"}

However, for the data below, how will I be able to get the name "NO_1"? Since its in an array, I want to be able to extract the data in it, and I'm thinking of getting the name of it first.

{   
    "SAMPLE": [
      {
        "NO_1": [
          {
            "RESULT": [
              {
                "NUMBER": 1,
                "F_NAME": "JOHN",
                "L_NAME": "SMITH"
                },
              {
                "NUMBER": 2,
                "F_NAME": "WILL",
                "L_NAME": "JONES"
                }
            ]
          }
        ]
      },
      {
        "NO_2": [
          {
            "RESULT": [
              {
                "NUMBER": 3,
                "F_NAME": "MARY",
                "L_NAME": "JANE"
                },
              {
                "NUMBER": 4,
                "F_NAME": "NEIL",
                "L_NAME": "STRONG"
                }
            ]
          }
        ]
      }
    ]
}

Any ideas?

DayIsGreen
  • 255
  • 1
  • 4
  • 16
  • 4
    You don't specifically need to use the `{"STRING"}` format. You can use `$objResult->SAMPLE[0]->NO_1` – Phil Jul 31 '18 at 06:38
  • "getting the name of it first"...you mean you don't know the name of the property, so you need to get a list of all the property names in the that object, which would include the "No_1" property? Is that what you mean? – ADyson Jul 31 '18 at 06:40
  • You can simply iterate through your object, if that is what you're asking. See [this answer](https://stackoverflow.com/a/4976649/372172) for example. If this is not what you want to do, you might have to tell us more details on what you're planning to do. – Koala Yeung Jul 31 '18 at 06:42
  • @ADyson - Yes. The object will be giving you a structure but for that very specific point of which I've mentioned, you will be provided with a name that might change. But regardless of it's name, the important is its data. – DayIsGreen Jul 31 '18 at 07:50
  • do you know the position of the object which contains that property within the "sample" array? Will it always be the first element of the array, for instance? That would be a good way to locate it. – ADyson Jul 31 '18 at 08:38

1 Answers1

0

There are two ways to get this. Either you can access direct index or iterate the loop to get all the members. Below is the example you can apply checks for error handling while accessing array indexes.

<?php
$json = '{   
    "SAMPLE": [
      {
        "NO_1": [
          {
            "RESULT": [
              {
                "NUMBER": 1,
                "F_NAME": "JOHN",
                "L_NAME": "SMITH"
                },
              {
                "NUMBER": 2,
                "F_NAME": "WILL",
                "L_NAME": "JONES"
                }
            ]
          }
        ]
      },
      {
        "NO_2": [
          {
            "RESULT": [
              {
                "NUMBER": 3,
                "F_NAME": "MARY",
                "L_NAME": "JANE"
                },
              {
                "NUMBER": 4,
                "F_NAME": "NEIL",
                "L_NAME": "STRONG"
                }
            ]
          }
        ]
      }
    ]
}';
$arr = json_decode($json,TRUE);
echo $arr['SAMPLE'][0]['NO_1'][0]['RESULT'][0]['F_NAME']; // Direct access
foreach ($arr as $key => $result){ // iterate in loop 
    foreach ($result as $key => $no) {
        foreach ($no['NO_1'] as $key => $res) {
            foreach ($res['RESULT'] as $key => $name) {
                echo $name['F_NAME'];
            }
        }
    }
}
die;
?>
piyush
  • 655
  • 4
  • 11