Bruno Albuquerque
Bruno Albuquerque

Reputation: 687

PHP - Creating Multidimensional Array in Foreach

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.

Upvotes: 1

Views: 76

Answers (1)

JCH77
JCH77

Reputation: 1363

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;
}

Upvotes: 1

Related Questions