0

my Json Data Something Like This

{
    "code": 0,
    "error": false,
    "message": "Team Detail",
    "data": {
        "trump": null,
        "team1": {
            "id": 150,
            "name": "South Africa",
            "sort_name": "SA",
            "image": "https://example.com"
        },
        "vise_captain": {
            "bowl_type": "Slow left-arm orthodox",
            "image": "https://example.com",
            "country": "South Africa",
            "player_pos": 4,
            "total_points": 106.0,
            "selected_as_vccaption": 3.7,
            "team_id": 150,
            "selected_as_trump": 0.0,
            "points": 16.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 4.08,
            "is_in_substitute_squad": "N",
            "player_id": 49083,
            "credits": 9.0,
            "dob": null,
            "bat_type": "LHB",
            "name": "D Elgar",
            "player_multiplier": 1.5,
            "position": "Batsman",
            "is_in_playing_squad": "Y",
            "selected_by": 75.94
        },
        "team2": {
            "id": 149,
            "name": "India",
            "sort_name": "IND",
            "image": "https://example.com"
        },
        "bowlers": [{
            "bowl_type": "Right-arm fast",
            "image": "https://example.com",
            "country": "South Africa",
            "player_pos": 3,
            "total_points": 146.0,
            "selected_as_vccaption": 15.85,
            "team_id": 150,
            "selected_as_trump": 0.0,
            "points": 68.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 22.94,
            "is_in_substitute_squad": "N",
            "player_id": 1953,
            "credits": 9.0,
            "dob": null,
            "bat_type": "LHB",
            "name": "K Rabada",
            "player_multiplier": 1.0,
            "position": "Bowler",
            "is_in_playing_squad": "Y",
            "selected_by": 94.52
        }, {
            "bowl_type": "Right-arm fast-medium",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 8,
            "total_points": 132.0,
            "selected_as_vccaption": 9.41,
            "team_id": 435,
            "selected_as_trump": 0.0,
            "points": 22.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 6.87,
            "is_in_substitute_squad": "N",
            "player_id": 607,
            "credits": 9.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "J Bumrah",
            "player_multiplier": 1.0,
            "position": "Bowler",
            "is_in_playing_squad": "Y",
            "selected_by": 93.37
        }, {
            "bowl_type": "Left-arm fast",
            "image": "https://example.com",
            "country": "South Africa",
            "player_pos": 10,
            "total_points": 134.0,
            "selected_as_vccaption": 3.46,
            "team_id": 435,
            "selected_as_trump": 0.0,
            "points": 72.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 2.35,
            "is_in_substitute_squad": "N",
            "player_id": 60394,
            "credits": 8.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "M Jansen",
            "player_multiplier": 1.0,
            "position": "bowler",
            "is_in_playing_squad": "Y",
            "selected_by": 52.02
        }, {
            "bowl_type": "Right-arm fast",
            "image": "https://example.com",
            "country": "South Africa",
            "player_pos": 9,
            "total_points": 156.0,
            "selected_as_vccaption": 9.88,
            "team_id": 150,
            "selected_as_trump": 0.0,
            "points": 4.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 4.66,
            "is_in_substitute_squad": "N",
            "player_id": 46131,
            "credits": 9.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "L Ngidi",
            "player_multiplier": 1.0,
            "position": "bowler",
            "is_in_playing_squad": "Y",
            "selected_by": 89.7
        }],
        "team_total_credits": 97.0,
        "captain": {
            "bowl_type": "",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 6,
            "total_points": 181.0,
            "selected_as_vccaption": 11.33,
            "team_id": 149,
            "selected_as_trump": 0.0,
            "points": 67.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 30.74,
            "is_in_substitute_squad": "N",
            "player_id": 661,
            "credits": 9.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "L Rahul",
            "player_multiplier": 2.0,
            "position": "Batsman",
            "is_in_playing_squad": "Y",
            "selected_by": 93.52
        },
        "team_total_points": 476.0,
        "team2_selected_player": 7,
        "name": 1,
        "allrounders": [{
            "bowl_type": "Right-arm offbreak",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 1,
            "total_points": null,
            "selected_as_vccaption": 4.42,
            "team_id": 149,
            "selected_as_trump": 0.0,
            "points": 27.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 3.15,
            "is_in_substitute_squad": "N",
            "player_id": 764,
            "credits": 8.5,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "H Vihari",
            "player_multiplier": 1.0,
            "position": "allrounder",
            "is_in_playing_squad": "Y",
            "selected_by": 22.84
        }, {
            "bowl_type": "Right-arm offbreak",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 2,
            "total_points": 57.0,
            "selected_as_vccaption": 3.39,
            "team_id": 149,
            "selected_as_trump": 0.0,
            "points": 56.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 3.12,
            "is_in_substitute_squad": "N",
            "player_id": 129,
            "credits": 9.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "R Ashwin",
            "player_multiplier": 1.0,
            "position": "Allrounder",
            "is_in_playing_squad": "Y",
            "selected_by": 80.76
        }],
        "id": 2431234,
        "wicketkeapers": [{
            "bowl_type": "",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 11,
            "total_points": 109.0,
            "selected_as_vccaption": 2.37,
            "team_id": 149,
            "selected_as_trump": 0.0,
            "points": 22.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 1.94,
            "is_in_substitute_squad": "N",
            "player_id": 1098,
            "credits": 9.0,
            "dob": null,
            "bat_type": "LHB",
            "name": "R Pant",
            "player_multiplier": 1.0,
            "position": "wicketkeeper",
            "is_in_playing_squad": "Y",
            "selected_by": 85.23
        }],
        "batsmans": [{
            "bowl_type": "",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 6,
            "total_points": 181.0,
            "selected_as_vccaption": 11.33,
            "team_id": 149,
            "selected_as_trump": 0.0,
            "points": 67.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 30.74,
            "is_in_substitute_squad": "N",
            "player_id": 661,
            "credits": 9.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "L Rahul",
            "player_multiplier": 2.0,
            "position": "Batsman",
            "is_in_playing_squad": "Y",
            "selected_by": 93.52
        }, {
            "bowl_type": "Right-arm medium",
            "image": "https://example.com",
            "country": "South Africa",
            "player_pos": 7,
            "total_points": 109.0,
            "selected_as_vccaption": 1.6,
            "team_id": 150,
            "selected_as_trump": 0.0,
            "points": 12.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 0.64,
            "is_in_substitute_squad": "N",
            "player_id": 46139,
            "credits": 9.0,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "T Bavuma",
            "player_multiplier": 1.0,
            "position": "batsman",
            "is_in_playing_squad": "Y",
            "selected_by": 45.1
        }, {
            "bowl_type": "",
            "image": "https://example.com",
            "country": "India",
            "player_pos": 5,
            "total_points": 82.0,
            "selected_as_vccaption": 7.83,
            "team_id": 435,
            "selected_as_trump": 0.0,
            "points": 35.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 7.42,
            "is_in_substitute_squad": "N",
            "player_id": 617,
            "credits": 8.5,
            "dob": null,
            "bat_type": "Right-hand bat",
            "name": "M Agarwal",
            "player_multiplier": 1.0,
            "position": "batsman",
            "is_in_playing_squad": "Y",
            "selected_by": 52.45
        }, {
            "bowl_type": "Slow left-arm orthodox",
            "image": "https://example.com",
            "country": "South Africa",
            "player_pos": 4,
            "total_points": 106.0,
            "selected_as_vccaption": 3.7,
            "team_id": 150,
            "selected_as_trump": 0.0,
            "points": 16.0,
            "playing_squad_updated": "Y",
            "selected_as_caption": 4.08,
            "is_in_substitute_squad": "N",
            "player_id": 49083,
            "credits": 9.0,
            "dob": null,
            "bat_type": "LHB",
            "name": "D Elgar",
            "player_multiplier": 1.5,
            "position": "Batsman",
            "is_in_playing_squad": "Y",
            "selected_by": 75.94
        }],
        "team1_selected_player": 4
    },
    "server_date": 1641264399
}

Now I want To Print All Value For a Specific Array How can Go through With this

For Example In above JSon data I need All Data For Player_id

Suppose IF We Enter Player_id and get all values with player_id like,bowl_type,image ...etc. As its An Complex Data I try with this code but not Working

function search_object(array $object_array, $desired_number) {
    foreach($object_array as $one) {
        if($one->Player_id == $desired_number) {
            return $one;
        }
    }
    //no result found
    return null;
}
  • Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – ADyson Jan 04 '22 at 08:08

1 Answers1

1

You want to find all subarrays which contain an element with a certain key and a certain value. In the general case, the level of the subarray is also not known, and neither is a path via specific keys. Recursive iterators are ideal for such tasks.

I have a universal filter function for the search.

/**
* Returns all filtered elements including arrays as a new numeric key
*
* @param $arr array
* @param $filter callable
* @return array
*/
function arrayFilterRecursive(array $arr, $filter){
    if(! is_callable($filter)) return false;
    $res = array();
    $it =  new RecursiveIteratorIterator(
      new RecursiveArrayIterator($arr),RecursiveIteratorIterator::SELF_FIRST
    );
    foreach($it as $key => $current){
      if($filter($current, $key, $it)) {
        $res[] = $current;
      }
    }
    return $res;
}

A filter function is required for the special task here.

$key = 'player_id';
$value = 46139; 

$filter = function($current) use($key, $value){
    return  is_array($current) && isset($current[$key]) && $current[$key] == $value;
};

Now only the Json string has to be converted into an array and our function has to be called.

$arr = json_decode($json,true);

$result = arrayFilterRecursive($arr, $filter);

The result for the selected example is an array:

array (
  0 => 
  array (
    'bowl_type' => "Right-arm medium",
    'image' => "https://example.com",
    'country' => "South Africa",
    'player_pos' => 7,
    'total_points' => 109.0,
    'selected_as_vccaption' => 1.6,
    'team_id' => 150,
    'selected_as_trump' => 0.0,
    'points' => 12.0,
    'playing_squad_updated' => "Y",
    'selected_as_caption' => 0.64,
    'is_in_substitute_squad' => "N",
    'player_id' => 46139,
    'credits' => 9.0,
    'dob' => NULL,
    'bat_type' => "Right-hand bat",
    'name' => "T Bavuma",
    'player_multiplier' => 1.0,
    'position' => "batsman",
    'is_in_playing_squad' => "Y",
    'selected_by' => 45.1,
  ),
)

The filter function can easily be adapted for other tasks. With

$key = 'player_id';
$filter = function($current) use($key){
    return  is_array($current) && isset($current[$key]) ;
};

I get all sub-arrays that have a key 'player_id'.

jspit
  • 7,276
  • 1
  • 9
  • 17