1

I have the following function that loop through a series of entities to create a Multi-dimensional array to return a JSON:

public function calcularIndicadorBenchmark(int $idIndicador): array
{
    $indicador = $this->buscarPorId($idIndicador);
    $configGrupoBase = $this->entityManager->getRepository(ConfigGrupoBase::class)->findByIndicador($idIndicador);
    $configGrupo = $this->entityManager->getRepository(ConfigGrupo::class)->findByConfigBase(end($configGrupoBase)->getId());

    foreach ($configGrupo as $c) {
        $arrayGrupos[] = [
            'id' => $c->getId(),
            'nome' => $c->getNome(),
            'agrupamentos' => null
        ];

        $configGrupoAtributo = $this->entityManager->getRepository(ConfigGrupoAtributo::class)->findByConfigGrupo($c->getId());
        $agrupamento = $this->entityManager->getRepository(Agrupamento::class)->findByAtributo(end($configGrupoAtributo)->getId());

        foreach ($agrupamento as $a) {
            $arrayAgrupamentos[] = [
                'id' => $a->getId(),
                'nome' => $a->getNome(),
                'periodos' => null
            ];
        }
        $arrayGrupos[] = $arrayAgrupamentos;
    }

    $json['indicador'] = [
        'id' => $indicador->getId(),
        'nome' => $indicador->getNome(),
        'codigo' => $indicador->getCodigo(),
        'grupos' => $arrayGrupos
    ];
    return $json;
}

My code isn't returning any errors, but this isn't exactly what I expect/need. Agrupamentos need to receive the array that is bellow it, and I trying to make it work but can't.

{
    "indicador": {
        "id": 20,
        "nome": "Produtividade pessoal: Técnica",
        "codigo": "IN014",
        "grupos": [
            {
                "id": 1,
                "nome": "Até/Acima 125 mil exames ao mês",
                "agrupamentos": null
            },
            [
                {
                    "id": 1,
                    "nome": "até 5 milhões por mês",
                    "periodos": null
                },
                {
                    "id": 2,
                    "nome": "Acima de 5 milhões por mês",
                    "periodos": null
                }
            ],
            {
                "id": 99,
                "nome": "Teste",
                "agrupamentos": null
            },
            [
                {
                    "id": 1,
                    "nome": "até 5 milhões por mês",
                    "periodos": null
                },
                {
                    "id": 2,
                    "nome": "Acima de 5 milhões por mês",
                    "periodos": null
                }
            ]
        ]
    }
}

To make sure, this the result that I need to return, assuming the same data:

{
    "indicador":{
        "id":20,
        "nome":"Produtividade pessoal: Técnica",
        "codigo":"IN014"
    },
    "grupos":[
        {
            "id":1,
            "nome":"Até/Acima 125 mil exames ao mês",
            "agrupamentos":[
                {
                    "id":1,
                    "nome":"até 5 milhões por mês",

                },
                {
                    "id":2,
                    "nome":"Acima de 5 milhões por mês",

                }
            ]
        },
        {
            "id":99,
            "nome":"Teste",
            "agrupamentos":[
                {
                    "id":1,
                    "nome":"até 5 milhões por mês",
                    "periodos":null
                },
                {
                    "id":2,
                    "nome":"Acima de 5 milhões por mês",
                    "periodos":null
                }
            ]
        }
    ]
}

Thanks in advance and sorry for occasional english mistakes.

1 Answer 1

1

You don't push data in $grupo['agrupamentos'][], try this :

foreach ($configGrupo as $c) {

    // Variable created, easiest to handle
    $grupo = [
        'id' => $c->getId(),
        'nome' => $c->getNome(),
        'agrupamentos' => [] // Init with empty array
    ];

    //...

    foreach ($agrupamento as $a) {

        // Push each agrupamento to grupo agrupamentos
        $grupo['agrupamentos'][] = [ 
            'id' => $a->getId(),
            'nome' => $a->getNome(),
            'periodos' => null // Array too ?
        ];
    }

    $arrayGrupos[] = $grupo;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Almost there, friend. The second agrupamentos is now empty, I put the results of your solution in this link: pastebin.com/D5dbsqeW
I don't know why, could you post your updated script ?
Hum... The logic is good, I don't understand why the second $grupo['agrupamentos'][] is empty. You should try to display the content of $agrupamento = $this->entityManager->getRepository(Agrupamento::class)->findByAtributo(end($configGrupoAtributo)->getId()); with var_dump($agrupamento);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.