首页 > 解决方案 > 如何使用 preg_match_all 找出所有值

问题描述

我有一个复杂的数据字符串,我想让它简单我的复杂字符串看起来像这样

{"status":true,"object":{"team1":{"teamId":"115927","name":"United CC","shortName":"UCC","logoImage":"https:\/\/cricket.entitysport.com\/assets\/uploads\/2020\/06\/ucc-120x120.png","players":[{"playersId":"99237","firstName":"Abhimanyu Singh","middleName":"","lastName":"","title":"Abhimanyu Singh","shortName":"Abhimanyu Singh","country":"fi","photo":"","role":"wk","ply_fantasy_points":"0","points":"26.50","playing11":"1"},{"playersId":"99239","firstName":"Shyamal Joshi","middleName":"","lastName":"","title":"Shyamal Joshi","shortName":"Shyamal Joshi","country":"fi","photo":"","role":"bat","ply_fantasy_points":"0","points":"9.00","playing11":"1"},{"playersId":"99244","firstName":"Pramod Bagauly","middleName":"","lastName":"","title":"Pramod Bagauly","shortName":"Pramod Bagauly","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"19.50","playing11":"1"},{"playersId":"99245","firstName":"Neelesh Pandit","middleName":"","lastName":"","title":"Neelesh Pandit","shortName":"Neelesh Pandit","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"10.00","playing11":"1"},{"playersId":"99247","firstName":"Kumal Deshmukh","middleName":"","lastName":"","title":"Kumal Deshmukh","shortName":"Kumal Deshmukh","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"2.00","playing11":"1"},{"playersId":"99249","firstName":"Arpan Shukia","middleName":"","lastName":"","title":"Arpan Shukia","shortName":"Arpan Shukia","country":"fi","photo":"","role":"bowl","ply_fantasy_points":"0","points":"11.00","playing11":"1"},{"playersId":"99251","firstName":"Amit Pangarkar","middleName":"","lastName":"","title":"Amit Pangarkar","shortName":"Amit Pangarkar","country":"fi","photo":"","role":"bowl","ply_fantasy_points":"0","points":"2.00","playing11":"1"}]},"team2":{"teamId":"115929","name":"Prague CC Kings","shortName":"PCC","logoImage":"https:\/\/cricket.entitysport.com\/assets\/uploads\/2020\/06\/pcck-120x120.png","players":[{"playersId":"99292","firstName":" Ramakrishnan ","middleName":"","lastName":"Sundareswaran","title":"Sharan Ramakrishnan Sundareswaran","shortName":"Sharan ","country":"fi","photo":"","role":"bat","ply_fantasy_points":"0","points":"0.00","playing11":"1"},{"playersId":"99293","firstName":"Arun Ashokan","middleName":"","lastName":"","title":"Arun Ashokan","shortName":"Arun Ashokan","country":"fi","photo":"","role":"bat","ply_fantasy_points":"0","points":"33.00","playing11":"1"},{"playersId":"99295","firstName":"Prakash Sadasivan","middleName":"","lastName":"","title":"Prakash Sadasivan","shortName":"Prakash Sadasivan","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"14.50","playing11":"1"},{"playersId":"99296","firstName":"Martin Glew","middleName":"","lastName":"","title":"Martin Glew","shortName":"Martin Glew","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"16.00","playing11":"1"}]},"userTeamId":"4444","matchId":"4343","earnPoints":"6.50","teamNumber":"1","captainId":"99239","viceCaptainId":"99247","isLineup":1},"message":"players","errCode":"11111"}

我正在尝试以简单字符串的格式制作所有团队 ID 和所有玩家 ID

 {"115927":[99222,99224,99226,99228,99230,99233],"115929":[99257,99264,99265,99266,99268]}

我尝试了许多类型的 preg_match_all 函数,但它不起作用有没有办法用 preg_match_all 或任何其他函数来获得这个 foramt

标签: php

解决方案


正则表达式可能不是执行此操作的最佳方法,因为您正在寻找嵌套数据,除非您真的知道自己在做什么,否则这在正则表达式中可能真的很难管理。

取而代之的是,使用 json_decode 您可以将 json 处理为 PHP 中的数组并将其重组为您想要的格式。

<?php
$json = '{"status":true,"object":{"team1":{"teamId":"115927","name":"United CC","shortName":"UCC","logoImage":"https:\/\/cricket.entitysport.com\/assets\/uploads\/2020\/06\/ucc-120x120.png","players":[{"playersId":"99237","firstName":"Abhimanyu Singh","middleName":"","lastName":"","title":"Abhimanyu Singh","shortName":"Abhimanyu Singh","country":"fi","photo":"","role":"wk","ply_fantasy_points":"0","points":"26.50","playing11":"1"},{"playersId":"99239","firstName":"Shyamal Joshi","middleName":"","lastName":"","title":"Shyamal Joshi","shortName":"Shyamal Joshi","country":"fi","photo":"","role":"bat","ply_fantasy_points":"0","points":"9.00","playing11":"1"},{"playersId":"99244","firstName":"Pramod Bagauly","middleName":"","lastName":"","title":"Pramod Bagauly","shortName":"Pramod Bagauly","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"19.50","playing11":"1"},{"playersId":"99245","firstName":"Neelesh Pandit","middleName":"","lastName":"","title":"Neelesh Pandit","shortName":"Neelesh Pandit","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"10.00","playing11":"1"},{"playersId":"99247","firstName":"Kumal Deshmukh","middleName":"","lastName":"","title":"Kumal Deshmukh","shortName":"Kumal Deshmukh","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"2.00","playing11":"1"},{"playersId":"99249","firstName":"Arpan Shukia","middleName":"","lastName":"","title":"Arpan Shukia","shortName":"Arpan Shukia","country":"fi","photo":"","role":"bowl","ply_fantasy_points":"0","points":"11.00","playing11":"1"},{"playersId":"99251","firstName":"Amit Pangarkar","middleName":"","lastName":"","title":"Amit Pangarkar","shortName":"Amit Pangarkar","country":"fi","photo":"","role":"bowl","ply_fantasy_points":"0","points":"2.00","playing11":"1"}]},"team2":{"teamId":"115929","name":"Prague CC Kings","shortName":"PCC","logoImage":"https:\/\/cricket.entitysport.com\/assets\/uploads\/2020\/06\/pcck-120x120.png","players":[{"playersId":"99292","firstName":" Ramakrishnan ","middleName":"","lastName":"Sundareswaran","title":"Sharan Ramakrishnan Sundareswaran","shortName":"Sharan ","country":"fi","photo":"","role":"bat","ply_fantasy_points":"0","points":"0.00","playing11":"1"},{"playersId":"99293","firstName":"Arun Ashokan","middleName":"","lastName":"","title":"Arun Ashokan","shortName":"Arun Ashokan","country":"fi","photo":"","role":"bat","ply_fantasy_points":"0","points":"33.00","playing11":"1"},{"playersId":"99295","firstName":"Prakash Sadasivan","middleName":"","lastName":"","title":"Prakash Sadasivan","shortName":"Prakash Sadasivan","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"14.50","playing11":"1"},{"playersId":"99296","firstName":"Martin Glew","middleName":"","lastName":"","title":"Martin Glew","shortName":"Martin Glew","country":"fi","photo":"","role":"all","ply_fantasy_points":"0","points":"16.00","playing11":"1"}]},"userTeamId":"4444","matchId":"4343","earnPoints":"6.50","teamNumber":"1","captainId":"99239","viceCaptainId":"99247","isLineup":1},"message":"players","errCode":"11111"}';

// turn this into an array
$array = json_decode($json, true);

$formatted = [];

foreach ($array['object'] as $team) {
    $teamId = $team['teamId'];

    foreach ($team['players'] as $player) {
        $formatted[$teamId][] = $player['playersId'];
    }
}

// $formatted now contains the format {teamId: [player1, player2], team2: [player3, player4]}
echo json_encode($formatted);

使用上面的代码,您将拥有一个$formatted数组,其中现在包含您希望格式的数据。使用json_encode您可以将其恢复为 json 格式的字符串。


推荐阅读