S
S
SET12021-05-14 16:27:14
PHP
SET1, 2021-05-14 16:27:14

Can't parse json. Can you help?

It is necessary to parse json through foreach.

Did it like this:

$arr = json_decode('[
  {
    "name": "test1",
  },
  {
    "name": "test2",
  }
]', true);
            
            foreach($arr as $item) {
                echo $item['name'];
            }


after executing the code, the page is empty, that is, there is no answer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
profesor08, 2021-05-14
@SET1

Wrong format. Extra commas. There must be no commas before the closing brace.

V
Valery Matvienko, 2021-05-14
@Midas

Well, firstly, your JSON is incorrectly described. It must be so...

$arr = json_decode(' {"my_array": [
  {
    "name": "test1"
  },
  {
    "name": "test2"
  }
]}', true);

Secondly, learn how to display errors in PHP and print different structures - the function print_r($arr);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question