Create array with string in php?

Asked

Viewed 310 times

4

I have a variable text and wanted to create an array with it, is it possible? follows the variable:

$texto = "forma=3&banco=100&agencia=200&conta=300&cheque=404";

the way out that desire would be is:

array(
'forma' => '3',
'banco' => '100',
'agencia' => '200',
'conta' => '300',
'cheque' => '404'
);

1 answer

5


There’s a PHP function for this, it’s called parse_str and it works like this:

$texto = "forma=3&banco=100&agencia=200&conta=300&cheque=404";
parse_str($texto, $array);

var_dump($array);

Upshot:

array(5) { 
  ["forma"]=> string(1) "3" 
  ["banco"]=> string(3) "100" 
  ["agencia"]=> string(3) "200" 
  ["conta"]=> string(3) "300" 
  ["cheque"]=> string(3) "404" 
}
  • 1

    perfect! exactly what I needed.

Browser other questions tagged

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