search for a given value php array

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error search for a given value php array on this date .

I don’t remember how I can search inside an array created from a JSON in php. I need to get all the information of an array element if the searched value is found. The input array is composed from the italian players data, I need to be able to create two arrays that are used in a script for a game simulation. For example, I wanna take all the players that will have the Juventus value in the team_name field and all the oppisite team players.

here is the players array data structure:

[{
"player_id":1,
"player_surname":"Ronaldo",
"player_name":"Cristiano",
"player_number":7,
"role_id":8,
"team_id":1010226,
"team_name":"Juventus",
"team_abbreviation":"JUV",
"opponent_team_id":1010220,
"opponent_team_name":"Napoli",
"opponent_team_abbreviation":"NAP",
"game_round_id":2,
"game_round_number":2,
"is_home":true,
"game_date":"2019-08-31 20:45:00",
"game_status_id":0,
"active":true,
"role_abbreviation":"A",
"role_name":"Attaccante",
"is_coach":false,"lineup_id":1,
"lineup_name":"Titolare",
"quotation":64,
"likelihood":1,
"sky_idx":100,
"total_score":6.5,
"avg_3_score":6.5,
"avg_score":6.5,
"game_played":false,
"percentage":null
}]

It will be useful array_search() or what set of functions?

I only have for now writed these lines of code, I need some guidance on how to proceed.

$home = [];
$away = [];

$p = json_decode(file_get_contents( GAME_DATA_PATH.'players_.json' ), true);

If I wanna to fit this json sample schema, how I can do? a foreach loop will be enough?

JSON SAMPLE

$lineup_sample = <<<JSON
{
   "players":{
      "home":[
         {
            "name":"Pizarro",
            "asset":"bm-pizarro.jpg",
            "origin":"Peru",
            "height":"1.84m",
            "shirt":"14",
            "pos":"Forward",
            "dob":"36",
            "goals":1,
            "games":16,
            "x":110,
            "y":-190
         },
         {
            "name":"Robben",
            "asset":"bm-robben.png",
            "origin":"Holland",
            "height":"1.80m",
            "shirt":"10",
            "pos":"Forward",
            "dob":"32",
            "goals":19,
            "games":30,
            "x":-110,
            "y":-190
         },
         {
            "name":"Rilbery",
            "asset":"bm-rilbery.jpg",
            "origin":"France",
            "height":"1.70m",
            "shirt":"7",
            "pos":"Midfield",
            "dob":"32",
            "goals":9,
            "games":22,
            "x":150,
            "y":50
         },
         {
            "name":"Schweinsteiger",
            "asset":"bm-schweinsteiger.jpg",
            "origin":"Germany",
            "height":"1.87m",
            "shirt":"24",
            "pos":"Forward",
            "dob":"31",
            "goals":21,
            "games":3,
            "x":0,
            "y":100
         },
         {
            "name":"Martinez",
            "asset":"bm-martinez.jpg",
            "origin":"Spain",
            "height":"1.90m",
            "shirt":"8",
            "pos":"Midfield",
            "dob":"28",
            "goals":0,
            "games":2,
            "x":-150,
            "y":50
         },
         {
            "name":"Alaba",
            "asset":"bm-alaba.jpg",
            "origin":"Austria",
            "height":"1.80m",
            "shirt":"27",
            "pos":"Defence",
            "dob":"24",
            "goals":5,
            "games":27,
            "x":-200,
            "y":180
         },
         {
            "name":"Lahm",
            "asset":"bm-lahm.jpg",
            "origin":"Germany",
            "height":"1.70",
            "shirt":"21",
            "pos":"Defence",
            "dob":"32",
            "goals":2,
            "games":25,
            "x":200,
            "y":180
         },
         {
            "name":"Benatia",
            "asset":"bm-benatia.jpg",
            "origin":"France",
            "height":"1.87m",
            "shirt":"5",
            "pos":"Defence",
            "dob":"31",
            "goals":21,
            "games":1,
            "x":100,
            "y":300
         },
         {
            "name":"Dante",
            "asset":"bm-dante.jpg",
            "origin":"Brazil",
            "height":"1.87m",
            "shirt":"4",
            "pos":"Defence",
            "dob":"32",
            "goals":0,
            "games":34,
            "x":-100,
            "y":300
         },
         {
            "name":"Neuer",
            "asset":"bm-neuer.jpg",
            "origin":"Germany",
            "height":"1.93m",
            "shirt":"1",
            "pos":"Goalie",
            "dob":"29",
            "goals":0,
            "games":48,
            "x":0,
            "y":410
         }
      ],
      "away":[
         {
            "name":"Benzema",
            "asset":"rm-benzema.jpg",
            "origin":"France",
            "height":"1.87m",
            "shirt":"9",
            "pos":"Forward",
            "dob":"36",
            "goals":1,
            "games":16,
            "x":110,
            "y":-190
         },
         {
            "name":"Bale",
            "asset":"rm-bale.jpg",
            "origin":"Wales",
            "height":"1.83m",
            "shirt":"11",
            "pos":"Midfield",
            "dob":"26",
            "goals":19,
            "games":30,
            "x":-110,
            "y":-190
         },
         {
            "name":"carvajal",
            "asset":"rm-carvajal.jpg",
            "origin":"Spain",
            "height":"1.70m",
            "shirt":"15",
            "pos":"Defender",
            "dob":"32",
            "goals":9,
            "games":22,
            "x":150,
            "y":50
         },
         {
            "name":"Silva",
            "asset":"rm-silva.jpg",
            "origin":"Brazil",
            "height":"1.87m",
            "shirt":"16",
            "pos":"Forward",
            "dob":"22",
            "goals":21,
            "games":3,
            "x":0,
            "y":100
         },
         {
            "name":"Kroos",
            "asset":"rm-kroos.jpg",
            "origin":"Germany",
            "height":"1.82",
            "shirt":"8",
            "pos":"Midfield",
            "dob":"25",
            "goals":0,
            "games":2,
            "x":-150,
            "y":50
         },
         {
            "name":"Modric",
            "asset":"rm-modric.jpg",
            "origin":"Croatia",
            "height":"1.74m",
            "shirt":"19",
            "pos":"Midfield",
            "dob":"30",
            "goals":5,
            "games":27,
            "x":-200,
            "y":180
         },
         {
            "name":"Nacho",
            "asset":"rm-nacho.jpg",
            "origin":"Germany",
            "height":"1.79",
            "shirt":"18",
            "pos":"Defence",
            "dob":"25",
            "goals":2,
            "games":25,
            "x":200,
            "y":180
         },
         {
            "name":"Ramos",
            "asset":"rm-ramos.jpg",
            "origin":"Spain",
            "height":"1.83m",
            "shirt":"4",
            "pos":"Defence",
            "dob":"31",
            "goals":21,
            "games":1,
            "x":100,
            "y":300
         },
         {
            "name":"Pepe",
            "asset":"rm-pepe.jpg",
            "origin":"Brazil",
            "height":"1.88m",
            "shirt":"3",
            "pos":"Defence",
            "dob":"32",
            "goals":0,
            "games":34,
            "x":-100,
            "y":300
         },
         {
            "name":"Casillas",
            "asset":"rm-casillas.jpg",
            "origin":"Spain",
            "height":"1.85m",
            "shirt":"1",
            "pos":"Goalie",
            "dob":"34",
            "goals":0,
            "games":48,
            "x":0,
            "y":410
         }
      ]
   }
}
JSON;

Answer

Looking at the data structure you provided and your code snippet, I assume that you want to have two arrays: one with the players from the home team, and another one with the players from the away team.

This is how I would probably do it with the help of array_filter():

<?php

$json = <<<JSON
[{
"player_id":1,
"player_surname":"Ronaldo",
"player_name":"Cristiano",
"player_number":7,
"role_id":8,
"team_id":1010226,
"team_name":"Juventus",
"team_abbreviation":"JUV",
"opponent_team_id":1010220,
"opponent_team_name":"Napoli",
"opponent_team_abbreviation":"NAP",
"game_round_id":2,
"game_round_number":2,
"is_home":true,
"game_date":"2019-08-31 20:45:00",
"game_status_id":0,
"active":true,
"role_abbreviation":"A",
"role_name":"Attaccante",
"is_coach":false,"lineup_id":1,
"lineup_name":"Titolare",
"quotation":64,
"likelihood":1,
"sky_idx":100,
"total_score":6.5,
"avg_3_score":6.5,
"avg_score":6.5,
"game_played":false,
"percentage":null
}]
JSON;

$data = json_decode($json, true);

$homeTeam = array_filter($data, function (array $player) {
    return $player['is_home'] === true;
});

$awayTeam = array_filter($data, function (array $player) {
    return $player['is_home'] === false;
});

// exact search
$juventusPlayers = array_filter($data, function (array $player) {
    return $player['team_name'] === 'Juventus';
});

// case insensitive search
$juventusPlayers = array_filter($data, function (array $player) {
    return stripos($player['team_name'], 'juventus') !== false;
});

// Helper function
$filterByTeamName = function (array $data, string $teamName) {
    return array_filter($data, function (array $player) use ($teamName) {
        return stripos($player['team_name'], $teamName) !== false;
    });
};

$juventusPlayers = $filterByTeamName($data, 'juventus');

You can apply the same mechanics for other properties of a player.

I hope this helps, cheers!