Extract content Array for PHP variables

3

I have a cURL statement that invokes a WS. The response from this WS is stored through:

$reference = var_dump(json_decode($exec, true));

return $reference;

Given that I have this in one function, in the other file I read as follows:

$var = ob_get_clean();

This is where I echo and give the result below:

array(4) {
  ["InvoiceIdOut"]=>
  int(945)
  ["FiscalDocumentNumber"]=>
  string(7) "AAAA945"
  ["InvoiceURL"]=>
  string(34) "http://teste.pt/maistestes/AAAA945"
  ["ErrorMessage"]=>
  NULL
}

However, I'm not realizing how I can pull data and save it in variables.

    
asked by anonymous 18.01.2017 / 16:37

7 answers

2

Command list or extract

PHP has a command called list that associates a variable tuple with the values of an array. If the array, as in your example is associative (key-value), you can use the command extract .

Using your sample array, if you use extract :

extract($seu_array);

It will automatically create the variables with the key names:

$InvoiceIdOut = 879
$FiscalDocumentNumber = "AAAA879"
$InvoiceURL = "http://teste.pt/maistestes/AAAA879"
$ErrorMessage = null
    
18.01.2017 / 16:55
2

The error is where you use var_dump() .

Look at documentation of var_dump() and you will see that this function returns no value.

The correct one would be:

$reference = json_decode($exec, true);
var_dump($reference);
return $reference;

So the return of json_decode will be stored in the variable $reference instead of being passed to var_dump and be discarded.

    
18.01.2017 / 17:30
1

I do not know what the name of your array is, but pretending it is this way:

    $array= array(
  "InvoiceIdOut"=>879,
  "FiscalDocumentNumber"=>"AAAA879",
  "InvoiceURL"=>"http://teste.pt/maistestes/AAAA879",
  "ErrorMessage"=> NULL
);

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

echo $array['InvoiceIdOut'];

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($array);
 echo $InvoiceIdOut;

I hope I have helped

    
18.01.2017 / 16:49
1

You can use the extract () function:

Type like this:

<?php
$foo = array(
    "InvoiceIdOut" => 879,
    "FiscalDocumentNumber" => "AAAA879",
    "InvoiceURL" => "http://teste.pt/maistestes/AAAA879",
    "ErrorMessage" => null,
    );
extract($foo);
echo "\$InvoiceIdOut = $InvoiceIdOut</br>\$FiscalDocumentNumber = $FiscalDocumentNumber</br>\$InvoiceURL = $InvoiceURL</br>\$ErrorMessage = $ErrorMessage</br>";
?>
//saida
$InvoiceIdOut= 879 
$FiscalDocumentNumber = AAAA879
$InvoiceURL = http://teste.pt/maistestes/AAAA879
$ErrorMessage = 
    
18.01.2017 / 17:19
0

A simple way is to do: $var = $array["valor da chave"] .

An example, where $array would be the name of array in your code.


Code:

<?php

$array = array (
  "InvoiceIdOut" => 879,
  "FiscalDocumentNumber" => "AAAA879",
  "InvoiceURL" => "http://teste.pt/maistestes/AAAA879",
  "ErrorMessage" => NULL
);

$InvoiceIdOut = $array["InvoiceIdOut"];
$FiscalDocumentNumber = $array["FiscalDocumentNumber"];
$InvoiceURL = $array["InvoiceURL"];
$ErrorMessage = $array["ErrorMessage"];

echo '<p> InvoiceIdOut: ' . $InvoiceIdOut . '</p>';
echo '<p> FiscalDocumentNumber: ' . $FiscalDocumentNumber . '</p>';
echo '<p> InvoiceURL: ' . $InvoiceURL . '</p>';
echo '<p> ErrorMessage: ' . $ErrorMessage . '</p>';

?>


Output:

InvoiceIdOut: 879

FiscalDocumentNumber: AAAA879

InvoiceURL: http://teste.pt/maistestes/AAAA879

ErrorMessage: 
    
18.01.2017 / 16:59
0

Thank you for your help. I was not really doing echo outside the function but rather the var_dump inside the function.

So here's the right way:

Function that receives WS response:

$reference = json_decode($exec, true);
return $reference;

Out of function:

$reference = Business::check(...Variáveis...)

echo $reference['InvoiceIdOut'];
echo $reference['FiscalDocumentNumber'];
echo $reference['InvoiceURL'];

Thank you once again for the help.

    
18.01.2017 / 18:19
0

Have you tried this?

$array = ["InvoiceIdOut" => 879, "FiscalDocumentNumber"=>"AAAA879","InvoiceURL"=>"http://teste.pt/maistestes/AAAA879", "ErrorMessage"=>NULL]

$invoiceIdOut = $array[0];
$fiscalDocumentNumber = $array[1];
$invoiceURL = $array[2];
$errorMessage = $array[3];
    
18.01.2017 / 16:48