Getting one array from an object? Json decode?

I have a json file that I decode using json_decode. A json file is an object that contains two arrays. I only need the first array, but it's hard for me to figure out how to do this.

Json file

{
   "app":{
      "available":{
         "stats":[
            {
               "name":"the name",
               "at":"url"
            },
            {
               "name":"the name",
               "at":"url"
            }
         ],
         "stats2":[
            {
               "name":"the name",
               "at":"url"
            },
            {
               "name":"the name",
               "at":"url"
            }
         ]
      }
   }
}

I use

foreach($data3['app']['available'] as $name => $value)
{
    foreach($value as $entry)
    {
        echo $entry['name'];
    }
}

As a result, I get each name from the array stats1 and stats2. I only need names from the stats1 array, not stats2. How can this be achieved?

+4
source share
1 answer

because in app-> two arrays are available: statistics and statistics2

If you are only interested in statistics, why not give it a try:

foreach($data3['app']['available']['stats'] as $name => $value)

__ __ UPDATE

Try it please

$in = '{"app":{"available":{"stats": [{"name":"the name","at":"url"},{"name":"the name", "at":"url"}],"stats2":[{"name":"the name","at":"url"},{"name":"the name","at":"url"}]}}}';

$obj = (array) json_decode($in, true);

foreach($obj['app']['available']['stats'] as $value)
{
foreach($value as $e => $v)
    {
     echo ($value['name'] );
     echo ("\r");
    }

}
+2
source

Source: https://habr.com/ru/post/1598140/


All Articles