douxun7992 2011-11-23 11:03
浏览 35
已采纳

PHP和PayPal IPN数组

I am getting PayPal's IPN in array form like this:

$ipn = array(
    'mc_gross' => '15.50',
    'protection_eligibility' => 'Eligible',
    'address_status' => 'confirmed',
    'item_number1' => '1',
    'tax' => '0.00',
    'item_number2' => '2',
    'payer_id' => 'XXXXXXXXXXXXX',
    'address_street' => '1 Main St',
    'payment_date' => '02:41:18 Nov 23, 2011 PST',
    'payment_status' => 'Completed',
    'charset' => 'windows-1252',
    'address_zip' => '95131',
    'mc_shipping' => '0.00',
    'mc_handling' => '0.00',
    'first_name' => 'Test',
    'mc_fee' => '0.75',
    'address_country_code' => 'US',
    'address_name' => 'Test User',
    'custom' => '',
    'payer_status' => 'verified',
    'business' => 'receiver@example.com',
    'address_country' => 'United States',
    'num_cart_items' => '2',
    'mc_handling1' => '0.00',
    'mc_handling2' => '0.00',
    'address_city' => 'San Jose',
    'payer_email' => 'payer@example.com',
    'mc_shipping1' => '0.00',
    'mc_shipping2' => '0.00',
    'txn_id' => '8RE82733S8684874F',
    'payment_type' => 'instant',
    'last_name' => 'User',
    'address_state' => 'CA',
    'item_name1' => 'trilogy2',
    'receiver_email' => 'receiver@example.com',
    'item_name2' => 'trilogy1',
    'payment_fee' => '0.75',
    'quantity1' => '1',
    'quantity2' => '1',
    'receiver_id' => 'XXXXXXXXXXXXX',
    'txn_type' => 'cart',
    'mc_gross_1' => '5.55',
    'mc_currency' => 'USD',
    'mc_gross_2' => '9.95',
    'residence_country' => 'US',
    'transaction_subject' => 'Shopping Carttrilogy2trilogy1',
    'payment_gross' => '15.50',
    '' => ''
    );

In this case customer has ordered only 2 items: 'item_name1' and 'item_name2'

Obviously, a customer can order X number of products and PayPal will include an 'item_nameX' in the notification data. Is there a way to go through the array and extract all names and assign them to variables so I will have something like:

$ordered_item1 = $ipn['item_name1'];
$ordered_item2 = $ipn['item_name2'];
...
$ordered_itemXYZ = $ipn['item_nameXYZ'];

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dousao2186 2011-11-23 14:33
    关注

    If you have data named with a prefix followed by a count, you should be using an array, not separate variables. To fill an array with the item information:

    foreach ($ipn as $key => $value) {
        if (preg_match('/(?P<field>[a-z]+)_?(?P<i>\d+)/', $key, $parts)) {
            $ordered_items[(int) $parts['i']][$parts['field']] = $value;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统