Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validate multiple subarray's content PHP

I would know how can I remove the empty value from the array I am creating from BD, I tried using the function array_filter like I saw in another questions, but got no lucky…

Could you tell me how to remove?

Here is the code that generates the array:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

while($row = $stm->fetch()) {

    if ($row['id_tipo_profissional'] == '1') {
    //CRIA ARRAY DE DADOS DO TIPO PROFISSIONAL 1

        $id_tipo_profissional_1 =   $row['id_tipo_profissional'];
        $tipo_profissional_1    =   $row['tipo_profissional'];

        $profissionais_1[] = [
            'id_profissional' => $row['id'],
            'nome' => ucwords($row['nome'] . ' ' . $row['sobrenome'])
        ];

    } else if ($row['id_tipo_profissional'] == '2') {
    //CRIA ARRAY DE DADOS DO TIPO PROFISSIONAL 2

        $id_tipo_profissional_2 =   $row['id_tipo_profissional'];
        $tipo_profissional_2    =   $row['tipo_profissional'];

        $profissionais_2[] = [
            'id_profissional' => $row['id'],
            'nome' => ucwords($row['nome_do_escritorio'])
        ];

    }
}


$resultado =  array_filter([
    "tipo" => [
            $id_tipo_profissional_1 => [
                'id_tipo_profissional'      =>  $id_tipo_profissional_1,
                'nome_tipo_profissional'    =>  $tipo_profissional_1,
                "profissionais"             =>  $profissionais_1
            ],


            $id_tipo_profissional_2 => [
                'id_tipo_profissional'      =>  $id_tipo_profissional_2,
                'nome_tipo_profissional'    =>  $tipo_profissional_2,
                "profissionais"             =>  $profissionais_2
            ],
    ],
]);

return json_encode($resultado);

With this output:

{
  "tipo": {
    "1": {
      "id_tipo_profissional": "1",
      "nome_tipo_profissional": "advogado",
      "profissionais": [
        {
          "id_profissional": "22",
          "nome": "Joao Abreu"
        }
      ]
    },
    "": {
      "id_tipo_profissional": null,
      "nome_tipo_profissional": null,
      "profissionais": null
    }
  }
}

But this my desire output when the array is empty (using this data as an example):

{
  "tipo": {
    "1": {
      "id_tipo_profissional": "1",
      "nome_tipo_profissional": "advogado",
      "profissionais": [
        {
          "id_profissional": "22",
          "nome": "Joao Abreu"
        }
      ]
    }
  }
}

>Solution :

You can use conditionals :

$typo = [];

if($id_tipo_profissional_1){
    $typo[$id_tipo_profissional_1] = [
        'id_tipo_profissional'      =>  $id_tipo_profissional_1,
        'nome_tipo_profissional'    =>  $tipo_profissional_1,
        "profissionais"             =>  $profissionais_1
    ];
}

 if($id_tipo_profissional_2){
    $typo[$id_tipo_profissional_2] = [
        'id_tipo_profissional'      =>  $id_tipo_profissional_2,
        'nome_tipo_profissional'    =>  $tipo_profissional_2,
        "profissionais"             =>  $profissionais_2
    ];
}

$resultado['typo'] = $typo;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading