D
D
Danil2020-08-21 12:38:03
PHP
Danil, 2020-08-21 12:38:03

How to parse and find the desired value in Json?

I get this line from the server

[
  {
    "id": 963343,
    "name": "\u0424\u043e\u0440\u043c\u0430 \u0433\u043e\u043b\u043e\u0441\u0430",
    "name_eng": "Koe no katachi",
    "year": "2016"
  },
  {
    "id": 977743,
    "name": "\u0424\u043e\u0440\u043c\u0430 \u0432\u043e\u0434\u044b",
    "name_eng": "The Shape of Water",
    "year": "2017"
  },
  {
    "id": 493390,
    "name": "\u0413\u043e\u043b\u043e\u0441\u0430",
    "name_eng": "The Voices",
    "year": "2014"
  }
]


I need to find id knowing that name:Voice shape and year:2016
I think that json_decode is required on how to find it I don't know

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav, 2020-08-21
@Deletron247

$json = '[
  {
    "id": 963343,
    "name": "\u0424\u043e\u0440\u043c\u0430 \u0433\u043e\u043b\u043e\u0441\u0430",
    "name_eng": "Koe no katachi",
    "year": "2016"
  },
  {
    "id": 977743,
    "name": "\u0424\u043e\u0440\u043c\u0430 \u0432\u043e\u0434\u044b",
    "name_eng": "The Shape of Water",
    "year": "2017"
  },
  {
    "id": 493390,
    "name": "\u0413\u043e\u043b\u043e\u0441\u0430",
    "name_eng": "The Voices",
    "year": "2014"
  }
]';
$array = json_decode($json, true);

$result = array_filter($array, function($item){
    if ($item['name'] == 'Форма голоса' && $item['year'] == '2016') return $item;
});

var_dump($result);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question