Jump to content

Need to grab value from array output


Go to solution Solved by Jacques1,

Recommended Posts

Hi,I

 

I'm trying to setup Paypal Pro on a website - have everything working - but need to grab the 4th & 6th key values from the results given from the Paypal response and having difficulty with the task.

 

See the code that prints the response and the response sent from Paypal below - I need to grab the values for  [TRXRESULT] and [TRXRESPMSG]

echo('<pre>');
print_r($PayFlow->getResponse());
echo('</pre>');

and here's the response I get from Paypal on script execution - this is an example of a failed transaction:

 

Paypal Response

Array
(
    [RESULT] => 36
    [RPREF] => RPC5B24581A2
    [RESPMSG] => Transaction failed: Fail to obtain approval for the online transaction
    [TRXRESULT] => 23
    [TRXPNREF] => EUJPC36F2092
    [TRXRESPMSG] => Invalid account number: Unsupported Credit Card type
)

Again, I want to grab the values from [TRXRESULT] and [TRXRESPMSG] and assign them to local variables so i can work with them within the local php script.

 

Example: $trxresult  and $trxrespmsg

 

Here's part of the class (file name is Class.Payflow.php find on Github) that pertains to this snippet of code in question:

 

Class.Payflow.php

/**
   * @uses      Gets the response from Paypal.
   * @access    Public
   * @param     None.
   * @return    Array/String - Returns an array of Paypal's response or empty string if not return.
   * @example   $PayFlow->getResponse();
   */
  public function getResponse() {
    if($this->response) {
      return $this->response;
    }
    else {
      return '';
    }
  }
  

Thanks in advance for help w/this one!

 

Link to comment
https://forums.phpfreaks.com/topic/290935-need-to-grab-value-from-array-output/
Share on other sites

  • Solution

So you're asking how to access an element of an associative array?

$paypal_response = $PayFlow->getResponse();

var_dump($paypal_response['TRXRESULT']);

If you're just confused because of the bracket syntax, use var_dump() instead of print_r().

 

There's actually no need to put all elements into separate variables. The values in the array are just as good as any other value.

  • Like 1

Hi,

 

This works great and exactly what i need to get the data I want - specifically.

However, I'm having trouble assigning those var_dumps to local variables - see example below which yields nothing:

$var1 = var_dump($paypal_response['TRXRESULT']);
echo $var1;    // doesn't produce any value for $var1

I need to assign the results to variables so i can work with them to insert / update MySQL database.

I tried var_export and other methods but unable to get the results/value(s) assigned as with this example.

 

Thx for help - really appreciated!

Edited by n1concepts

Never mind - think I got it figured out...

Need to clean up the results but not got value from var_dump into local var as shown below:

ob_start();
var_dump($api_response1['RESULT']);
$var1 = ob_get_contents();
ob_end_clean();  

echo $var1;

Thanks again for help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.