<?php

$json = '{
  "data": {
    "card": {
      "current_phase": {
        "id": "3134719",
        "name": "Phase Final"
      },
      "child_relations": [
        {
          "cards": [
            {
              "id": "6405893",
              "current_phase": {
                "id": "3147728",
                "name": "Concluído 2"
              },
              "child_relations": [
                {
                  "pipe": {
                    "id": "458138"
                  },
                  "name": "Pipe 3 Conexão",
                  "cards": [
                    {
                      "id": "6407049",
                      "current_phase": {
                        "id": "3151152",
                        "name": "pipe 3 phase 1"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  }
}';

$nivel = 0;
$nivelMax = 0;
$posLim = 0;
for ($i = 0; $i < strlen ($json); $i++) {
	$car = substr($json, $i, 1);
	if ($car == '{') {
		$nivel++;
		if ($nivel > $nivelMax) {
			$nivelMax = $nivel;
			$posIniNivelMax = $i;
		}
	}
	if ($car == '}') {
		if ($nivel == $nivelMax)
			$posFimNivelMax = $i;
		$nivel--;
	}
	if ($car == ',') {
		$posLimAnt = $posLim;
		$posLim = $i;
	}
};
echo substr($json, $posLimAnt+1, $posFimNivelMax - $posLimAnt + 1) . '<br>';