How do I access an array and export the variables?

1

I have a code that receives data via $_POST of a form as follows:

...
$certificate = $_POST['certificate'];
...

If you make a print_r($certificate) , the result is as follows:

Array (
        ['cliente'] => White Martins Gases Industriais Ltda 
        ['entrega'] => 2017-04-11
        ['utilizacao'] => Oxigênio
        ['norma'] => White Martins - PR029
      )

The fields in the form are as follows:

<select type="select" class="form-control" name="certificate['cliente']" id="cliente">
<input type="date" class="form-control" name="certificate['entrega']" id="entrega"> 
<select type="select" class="form-control" name="certificate['utilizacao']" id="utilizacao">
<select type="select" class="form-control" name="certificate['norma']" id="norma">

How to assign the values of this array to 4 separate variables, type:

$cliente = ????;
$entrega = ????;
$utilizacao = ????;
$norma = ????;

I'm not sure how to access the array.

    
asked by anonymous 11.04.2017 / 17:05

3 answers

1

You can use list() . However, since you do not use numeric indexes, you will need to use array_values , for example:

list($cliente, $entrega, $utilizacao, $norma) = array_values($_POST['certificate']);

Try this here .

Then you can use $cliente , $entrega normally.

    
14.04.2017 / 00:05
3

One way to access the content would be to tell the array name, and its index, like this:

echo $certificate['cliente'];

Another way would also be to use extract , which performs the function you want, to extract the content and transforms into variables, like this:

 extract($certificate);
 echo $cliente;

A very simple example for your case, using array:

HTML

<form action="" method="post"><select type="select" class="form-control" name="certificate[]" id="cliente" value="cliente">
    <input type="text" class="form-control" name="certificate[entrega]" id="entrega" value="entrega"> 
    <input type="text" class="form-control" name="certificate[utilizacao]" value="utilizacao">
    <input type="submit">
</form>

Already in PHP, you would get the data as follows:

<?php 
    $certificate=$_POST['certificate'];
    echo $certificate['entrega'];
?>

I hope I have helped

    
11.04.2017 / 17:47
1

Have you tried this?

$cliente = $certificate['cliente'];
$entrega = $certificate['entrega'];
$utilizacao = $certificate['utilizacao'];
$norma = $certificate['norma'];
    
11.04.2017 / 17:26