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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方
  • ¥15 关于生产日期批次退货退款,库存回退的问题