Define id of the php array()

Asked

Viewed 233 times

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 put the inputs in that way: camposdb[$row_fetch['id']][] that bring me the result above.

What I need to do is this...

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

.

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

$id = $campodb[0][???]

1 answer

5


To get your keys array of the above example 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 that example illustrates well and solves the doubt of the key search array.

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

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

Example

  • My data comes from $_POST, in this case the name of the fields are: camposdb[<? echo $row_fetch['id'];?>][]

  • 1

    @Andrébaill I followed the example just above, and now I don’t understand how your data comes, so an issue in your question would help understand...

  • I edited my question, maybe you understand a little better :)

  • In this case, I can put $array = $_POST; ?:

  • @Andrébaill to get the keys that can be dynamic or not the two codes are there as an example of the array you passed. The function array_keys takes the keys that in case is 51 and 60 with these keys inside the array main I do array['camposdb'][$key] which are all the information of the selected id. In the example for the data variable will get all the information of each id.

  • Yes you can. no problem @Andrébaill ...

  • 1

    Okay, it worked out, that’s exactly what I needed @Virgilionovic, thank you very much!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.