How to transform Array into distinct variables?

Asked

Viewed 941 times

4

I have the following array that returns me the following values:

(
    [cliente] => Array
        (
            [0] => Array
                (
                    [Code] => 1
                    [Name] => a
                )

            [1] => Array
                (
                    [Code] => 2
                    [Name] => b
                )  
        )  
)

There’s some way I can pick up for each Code turn to a variable $stnCode and for each Name transform to $stnName?

Because then I want to put it in a database, where the table Cliente receives Code and Name.

  • What do you mean? you want a variable that will receive several different values?

  • @Juniornunes7 more or less need to insert in the database that array in a table, which has Code and Name as well, so for each array will be inserted in a table Row.

1 answer

4


You can go through $dados indicating the sub-array cliente:

$dados = array('cliente' => array('0' => array('Code' => 1, 'Name' => 'a'),
                                  '1' => array('Code' => 2, 'Name' => 'b')));

foreach ($dados['cliente'] as $cliente) {
    $stnCode = $cliente['Code'];
    $stnName = $cliente['Name'];

    // Use $stnCode e $stnName aqui...

    echo $stnCode . ":" . $stnName . "\n";
}

See DEMO

Browser other questions tagged

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