فيما يلي طريقة لفك تشفير الJSON إلى مصفوفة PHP ثنائية الأبعاد وتحويلها إلى نفس التنسيق الذي تم عرضه في السؤال:
php
$json_data = '{
"results" : [
[
[[3,5], [2,4], [6,3], [2,3], [1,5], [5,3], [7,2], [1,2]],
[[1,2], [3,4], [5,6], [7,8]],
[[9,1], [8,2]],
[[1,3]]
],
[
[[5,1], [1,2], [3,2], [6,9]],
[[8,2], [1,2], [6,2], [1,3]],
[[1,2], [3,1]],
[[3,0], [1,9]],
[[3,2]],
[[4,2]]
],
[
[[3,8], [1,2]],
[[2,1]]
]
]
}';
$data = json_decode($json_data, true);
$winner_results = [];
$loser_results = [];
$finals_results = [];
// Decode winner bracket
foreach ($data['results'][0] as $round => $matches) {
foreach ($matches as $match => $teams) {
$winner_results[] = ["match" . ($round * count($matches) + $match + 1)] + $teams;
}
}
// Decode loser bracket
foreach ($data['results'][1] as $round => $matches) {
foreach ($matches as $match => $teams) {
$loser_results[] = ["match" . ((count($data['results'][0]) + $round) * count($matches) + $match + 1)] + $teams;
}
}
// Decode finals
foreach ($data['results'][2] as $round => $matches) {
foreach ($matches as $match => $teams) {
$finals_results[] = ["match" . ((count($data['results'][0]) + count($data['results'][1]) + $round) * count($matches) + $match + 1)] + $teams;
}
}
// Print the decoded PHP arrays
echo "\$winner_results = " . var_export($winner_results, true) . ";\n\n";
echo "\$loser_results = " . var_export($loser_results, true) . ";\n\n";
echo "\$finals_results = " . var_export($finals_results, true) . ";\n\n";
// Encoding PHP arrays to the exact same JSON format
$encoded_json = json_encode(["results" => [$winner_results, $loser_results, $finals_results]], JSON_PRETTY_PRINT);
echo "Encoded JSON:\n" . $encoded_json . "\n";
?>
تأكد من تحديث البيانات الموجودة في المتغير $json_data
ببيانات JSON الخاصة بك إذا كنت ترغب في اختبار معلومات مختلفة.
المزيد من المعلومات
بالطبع، سأوفر المزيد من المعلومات بخصوص فك تشفير الـ JSON وتحويله إلى مصفوفات PHP بناءً على البيانات المقدمة. يتيح الكود السابق إجراء هذا العمل بفعالية. دعني أقدم لك شرحًا تفصيليًا للكود:
أولاً وقبل كل شيء، يتم استخدام دالة json_decode
في PHP لتحليل النص JSON وتحويله إلى مصفوفة PHP. الخيار true
الثاني يضمن أن يتم تحويل الكائنات إلى مصفوفات PHP.
php$data = json_decode($json_data, true);
ثم يتم تحليل الجدول الزمني للفائزين والخاسرين والمباريات النهائية. يتم ذلك باستخدام حلقتين متداخلتين، أولهما للدور (rounds) والثاني للمباريات (matches) في كل جولة.
phpforeach ($data['results'][0] as $round => $matches) {
foreach ($matches as $match => $teams) {
$winner_results[] = ["match" . ($round * count($matches) + $match + 1)] + $teams;
}
}
الجزء السابق يقوم بتحليل جولة الفائزين واستخراج معلومات المباريات، حيث يتم إنشاء مصفوفة بترتيب يحتوي على اسم المباراة والفريقين.
الخطوات المتشابهة تتكرر للجولة الثانية (الخاسرين) والجولة الثالثة (النهائيات).
php// Decode loser bracket
foreach ($data['results'][1] as $round => $matches) {
foreach ($matches as $match => $teams) {
$loser_results[] = ["match" . ((count($data['results'][0]) + $round) * count($matches) + $match + 1)] + $teams;
}
}
// Decode finals
foreach ($data['results'][2] as $round => $matches) {
foreach ($matches as $match => $teams) {
$finals_results[] = ["match" . ((count($data['results'][0]) + count($data['results'][1]) + $round) * count($matches) + $match + 1)] + $teams;
}
}
أخيرًا، يتم طباعة المصفوفات النهائية بتنسيق قريب من الذي تم طلبه في السؤال:
phpecho "\$winner_results = " . var_export($winner_results, true) . ";\n\n";
echo "\$loser_results = " . var_export($loser_results, true) . ";\n\n";
echo "\$finals_results = " . var_export($finals_results, true) . ";\n\n";
وأخيرا، تم تشفير المصفوفات النهائية إلى تنسيق JSON بناءً على الطلب:
php// Encoding PHP arrays to the exact same JSON format
$encoded_json = json_encode(["results" => [$winner_results, $loser_results, $finals_results]], JSON_PRETTY_PRINT);
echo "Encoded JSON:\n" . $encoded_json . "\n";
بهذه الطريقة، يمكنك الآن فك تشفير JSON، وتحويله إلى مصفوفات PHP، والقيام بالعكس أيضًا.