fork download
  1. <?php
  2.  
  3. $json = '{
  4. "data": {
  5. "card": {
  6. "current_phase": {
  7. "id": "3134719",
  8. "name": "Phase Final"
  9. },
  10. "child_relations": [
  11. {
  12. "cards": [
  13. {
  14. "id": "6405893",
  15. "current_phase": {
  16. "id": "3147728",
  17. "name": "Concluído 2"
  18. },
  19. "child_relations": [
  20. {
  21. "pipe": {
  22. "id": "458138"
  23. },
  24. "name": "Pipe 3 Conexão",
  25. "cards": [
  26. {
  27. "id": "6407049",
  28. "current_phase": {
  29. "id": "3151152",
  30. "name": "pipe 3 phase 1"
  31. }
  32. }
  33. ]
  34. }
  35. ]
  36. }
  37. ]
  38. }
  39. ]
  40. }
  41. }
  42. }';
  43.  
  44. $nivel = 0;
  45. $nivelMax = 0;
  46. $posLim = 0;
  47. for ($i = 0; $i < strlen ($json); $i++) {
  48. $car = substr($json, $i, 1);
  49. if ($car == '{') {
  50. $nivel++;
  51. if ($nivel > $nivelMax) {
  52. $nivelMax = $nivel;
  53. $posIniNivelMax = $i;
  54. }
  55. }
  56. if ($car == '}') {
  57. if ($nivel == $nivelMax)
  58. $posFimNivelMax = $i;
  59. $nivel--;
  60. }
  61. if ($car == ',') {
  62. $posLimAnt = $posLim;
  63. $posLim = $i;
  64. }
  65. };
  66. echo substr($json, $posLimAnt+1, $posFimNivelMax - $posLimAnt + 1) . '<br>';
Success #stdin #stdout 0.01s 23500KB
stdin
Standard input is empty
stdout
                      "current_phase": {
                        "id": "3151152",
                        "name": "pipe 3 phase 1"
                      }
<br>