Extract Array content for PHP variables

Asked

Viewed 874 times

3

I have a Curl statement that invokes a WS. The answer to this WS is saved via:

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

return $reference;

Since 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 don’t understand how I can remove the data and store it in variables.

  • If this is the array resulting from the JSON conversion, the solution is a simple access to the variable that stores this array. $resultado['InvoiceIdOut']. If the answers below are not working it is likely that you have not kept the result of json_encode()

  • It seems to me that the error is in "var_dump" because it does not touch anything, it is a "void". Associate the direct variable to json_decode.

7 answers

2

Commando list or extract

PHP has a command called list which makes the association of a tuple of variables to the values of an array. If the array, as in your example is associative (value-key), you can use the command Extract.

Using your example array, if you use the extract:

extract($seu_array);

It will automatically create variables with key names:

$InvoiceIdOut = 879
$FiscalDocumentNumber = "AAAA879"
$InvoiceURL = "http://teste.pt/maistestes/AAAA879"
$ErrorMessage = null
  • You give me nothing back :\

  • Supplement your question with the code you wrote.

  • would not advise to use extract() because it pollutes the whole scope of the function, not to mention that it can give conflicts because they are data from an external source.

  • So many answers I didn’t even see your +1

  • @Pagotti, updated question

2

The mistake is where you use the var_dump().

Look at the documentation of var_dump() and you will see that this function does not return any value.

The right thing would be:

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

Thus the return of json_decode will be stored in the variable $reference instead of being passed on to var_dump and be disposed of.

  • I did as you mentioned and the result is the same as in the question.

  • Luis, to facilitate the people who are trying to help you do the following. 1. Show us what you get from WS 2. The code where you receive this string and how you do it & #Xa;3. Show more of your code, it’s hard to figure out what you have in the variable $exec, if there are any errors before this that we are not seeing. Because from what you asked, there is nothing wrong. Just access the information in the array.

1

I don’t know what the name of your array looks like:

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

One way to access the content would be to inform the name of the array,:

echo $array['InvoiceIdOut'];

Another way would also be using the Extract , that performs the function that Voce wants, of extracting the content and transforms into variables, thus:

 extract($array);
 echo $InvoiceIdOut;

I hope I’ve helped

  • Returns only one <

  • @Luisassunção do so $array= json_decode($exec, true); and then use the example of my answer

1

You can use the function Extract():

Kind of:

<?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 = 

0

Have you tried that?

$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];
  • The same result is returned as in @hard code.

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>';

?>


Exit:

InvoiceIdOut: 879

FiscalDocumentNumber: AAAA879

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

ErrorMessage: 
  • This result is returned to me: Invoiceidout: < Fiscaldocumentnumber: < Invoiceurl: < Errormessage:

  • @Luisa Asunción you changed the variable $array according to your code?

  • Yes, I’ve updated the question.

  • @Luisa Assunção see if it works by changing this line: $reference = var_dump(json_decode($exec, true)); for this: $reference = json_decode($exec, true);

  • If I don’t use var_dump I won’t create the array.

  • @Luísasunción the problem is probably in the way you are returning the WS data. Edit your question and post more information about your code so we can help you.

Show 1 more comment

0

I appreciate the help provided by all. I was not really doing echo out of the function but rather the var_dump inside the function.

So I share here the correct form:

Function receiving 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'];

Thanks again for the help.

Browser other questions tagged

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