0

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:

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"
        }
      ]
    }
  }
}
2
  • what if id_tipo_profissional is null and others not null? Commented Feb 24, 2022 at 12:27
  • Its impossible in the context on my data, if "id_tipo_profissional" is null all the others inside will be null too Commented Feb 24, 2022 at 12:28

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I am testing here hehe, thanks alot for your time

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.