Create array with string in php?

4

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

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

The output I wish would be:

array(
'forma' => '3',
'banco' => '100',
'agencia' => '200',
'conta' => '300',
'cheque' => '404'
);
    
asked by anonymous 30.09.2017 / 21:50

1 answer

5

There is a PHP function for this, 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);

Result:

array(5) { 
  ["forma"]=> string(1) "3" 
  ["banco"]=> string(3) "100" 
  ["agencia"]=> string(3) "200" 
  ["conta"]=> string(3) "300" 
  ["cheque"]=> string(3) "404" 
}
    
30.09.2017 / 21:53