Define array id () php

6

I have the following array ()

Array
(
    [camposdb] => Array
        (
            [51] => Array
                (
                    [0] => cpf_cnpj
                    [1] => nome_razaosocial
                    [2] => cod_cliente
                    [3] => cod_contrato
                    [4] => mensagem
                    [5] => mensagem
                    [6] => mensagem
                )

            [60] => Array
                (
                    [0] => celular
                    [1] => nome_razaosocial
                    [2] => nome_razaosocial
                    [3] => cod_contrato
                    [4] => mensagem
                    [5] => mensagem
                    [6] => telefone_r_1
                )

        )

)

In my form, I select the database records, and I put the inputs like this: camposdb[$row_fetch['id']][] which brings me the above result.

What I need to do is the following ...

UPDATE tabela SET (cpf_cnpj, nome_razaosocial....) WHERE id = 51

.

The question is: How do I define that 51 and 60 are ids and are they dynamic?

$id = $campodb[0][???]
    
asked by anonymous 08.09.2016 / 03:15

1 answer

5

To find the keys of your array from the example above use the array_keys to extract the two keys that are the id you need:

$array = array(
    "camposdb" => array(
        51 => array(
        ),
        60 => array(
        )
    )
);


$id = array_keys($array['camposdb']);
echo $id[0]; //51
echo $id[1]; //60

In this example illustrates well and solves the doubts of the search for the array key.

After editing, to do a dynamic search (I understand), use a foreach to get the keys and then inside you can get the information for each key , where variable $key is the bank id :

foreach(array_keys($array['camposdb']) as $key)
{
    $dados = $array['camposdb'][$key];
}

Example

    
08.09.2016 / 03:30