【问题标题】:decode Json And Getting All Value With Some Specific Key oR Object php解码 Json 并使用某些特定键或对象 php 获取所有值
【发布时间】:2023-04-04 00:50:01
【问题描述】:

我的 Json 数据是这样的

{
    "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
}

现在我想打印特定数组的所有值如何处理

例如在上面的 JSON 数据中,我需要 Player_id 的所有数据

假设如果我们输入 Player_id 并获取所有带有 player_id 的值,例如,bowl_type,image ...等。 作为一个复杂的数据,我尝试使用此代码但不工作

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;
}

【问题讨论】:

标签: php json multidimensional-array


【解决方案1】:

您想查找所有包含具有特定键和特定值的元素的子数组。在一般情况下,子数组的级别也是未知的,也不是通过特定键的路径。 Recursive iterators 非常适合此类任务。

我有一个通用的搜索过滤功能。

/**
* 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;
}

这里的特殊任务需要过滤功能。

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

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

现在只需要将 Json 字符串转换为数组,并调用我们的函数。

$arr = json_decode($json,true);

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

所选示例的结果是一个数组:

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,
  ),
)

过滤器功能可以很容易地适应其他任务。与

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

我得到所有具有键“player_id”的子数组。

【讨论】:

  • 先生,我想要所有 player_id 和相关数据,你能告诉我任何 forech 循环方法的简单方法
  • 从我的角度来看,带有 player_id 的子数组处于不同的级别。使用 foreach 循环无法轻松解决此问题。
  • 现在如何使用 player_id 获取所有值
  • @rinkesh1992:我已经添加到我的答案中。
猜你喜欢
  • 2017-06-07
  • 1970-01-01
  • 2018-11-02
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 2017-08-22
相关资源
最近更新 更多