php - how To find out all vaues with preg_match_all
Get the solution ↓↓↓i have a complex string of data and i wan to make it simple my complext string look like this
{"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"}
i am tring to making all team id and all players ids in a format like an simple string
{"115927":[99222,99224,99226,99228,99230,99233],"115929":[99257,99264,99265,99266,99268]}
i try many types preg_match_all function but its not work is there any way to get this foramt with preg_match_all or any other function
Answer
Solution:
Regex may not be the best way to do this as you're looking to nest the data and this can be really difficult to manage in regex unless you really know what you're doing..
Instead of this, using json_decode you can process the json as an array in PHP and restructure it into the format you want.
<?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);
Using the code above you will have a$formatted
array which now contains the data in the format you wish. Usingjson_encode
you can then get it back to a json formatted string.
Answer
Solution:
You can loop and extract all players:
foreach(json_decode($json, true)['object'] as $team) {
if(isset($team['teamId'])) {
$result[$team['teamId']] = array_column($team['players'], 'playersId');
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.