2012-01-28 100 views
0

我有這樣的$ _POST陣列從PayPal交易循環到數組並獲得唯一特定鍵值對

Array 
(
    [address_city] => San Jose 
    [address_country] => United States 
    [address_country_code] => US 
    [address_name] => Test User 
    [address_state] => CA 
    [address_status] => confirmed 
    [address_street] => 1 Main St 
    [address_zip] => 95131 
    [business] => [email protected] 
    [charset] => windows-1252 
    [custom] => 
    [first_name] => Test 
    [item_name1] => Product Name 1 
    [item_name2] => Product Name 6 
    [item_name3] => Product Name 8 
    [item_number1] => 1 
    [item_number2] => 6 
    [item_number3] => 8 
    [last_name] => User 
    [mc_currency] => USD 
    [mc_fee] => 0.82 
    [mc_gross] => 18.00 
    [mc_gross_1] => 14.00 
    [mc_gross_2] => 2.00 
    [mc_gross_3] => 2.00 
    [mc_handling] => 0.00 
    [mc_handling1] => 0.00 
    [mc_handling2] => 0.00 
    [mc_handling3] => 0.00 
    [mc_shipping] => 11.00 
    [mc_shipping1] => 11.00 
    [mc_shipping2] => 0.00 
    [mc_shipping3] => 0.00 
    [notify_version] => 3.4 
    [num_cart_items] => 3 
    [payer_email] => [email protected] 
    [payer_id] => TRCLJTHLNCJ7Q 
    [payer_status] => verified 
    [payment_date] => 22:52:56 Jan 27, 2012 PST 
    [payment_fee] => 0.82 
    [payment_gross] => 18.00 
    [payment_status] => Completed 
    [payment_type] => instant 
    [protection_eligibility] => Eligible 
    [quantity1] => 1 
    [quantity2] => 1 
    [quantity3] => 1 
    [receiver_email] => [email protected] 
    [receiver_id] => 74PV23B8KSK84 
    [residence_country] => US 
    [tax] => 0.00 
    [tax1] => 0.00 
    [tax2] => 0.00 
    [tax3] => 0.00 
    [test_ipn] => 1 
    [transaction_subject] => Shopping CartProduct Name 1Product Name 6Product Name 8 
    [txn_id] => 7BS85664SB906284D 
    [txn_type] => cart 
    [verify_sign] => AJ2IuqHp8G0lIxhedAqrwRQbv8fVA4-Gum9e7DMZmEWIFrljEwFCJDfP 
) 
1 

來,你可以看到,有這樣的鍵值對,

[items_name1] => Product Name 1 
[items_name2] => Product Name 2 
[items_name3] => Product Name 3 

這些密鑰不是恆定的,我的意思是,它從一個購物車來了,所以項目名稱取決於有多少物品被放置在購物車,購買。現在我的問題是,如何 循環到該職位數組,並只獲得item_names加上它們相應的值?,因爲我需要他們,並在數據庫表

+0

我的解決方案是更好 - 它分離每種產品,而不僅僅是項目名稱的一切。它甚至給你的產品數量(當然,正如它提到的,這個數字是在數組中提供的)。 – Cheery 2012-01-28 18:23:36

回答

4

循環保存在陣列中的每個元素,並檢查是否與鍵啓動「items_name」使用strposdocs。如果是這樣,請將其添加到$items陣列中。

$items = array(); 
foreach ($_POST as $key => $val) 
{ 
    if (strpos($key, 'items_name') === 0) { 
    $items[$key] = $val; 
    } 
} 
var_dump($items); 
2

更好的解決方案($數據是您的陣列)

$output = array(); 
    foreach($data as $field => $value) 
    { 
    if (preg_match('/(.*)(\d+)$/', $field, $match)) 
    { 
     $output[$match[2]][$match[1]] = $value; 
    } 
    }  
    print_r($output); 

輸出:

Array 
(
    [1] => Array 
     (
      [item_name] => Product Name 1 
      [item_number] => 1 
      [mc_gross_] => 14.00 
      [mc_handling] => 0.00 
      [mc_shipping] => 11.00 
      [quantity] => 1 
      [tax] => 0.00 
     ) 

    [2] => Array 
     (
      [item_name] => Product Name 6 
      [item_number] => 6 
      [mc_gross_] => 2.00 
      [mc_handling] => 0.00 
      [mc_shipping] => 0.00 
      [quantity] => 1 
      [tax] => 0.00 
     ) 

    [3] => Array 
     (
      [item_name] => Product Name 8 
      [item_number] => 8 
      [mc_gross_] => 2.00 
      [mc_handling] => 0.00 
      [mc_shipping] => 0.00 
      [quantity] => 1 
      [tax] => 0.00 
     ) 

) 
1

假定$測試作爲輸出數組,它包含的值。你可以這樣做:

$newValue = array(); 
foreach ($test as $testKey=>$testValue) { 
    if (strlen(strstr($testKey,'item_name')) >0 && preg_match_all("/.*?(\d+)$/", $testKey, $matches) > 0) { 
     $newValue[$testKey]=$testValue; 
    } 
} 

print_r($newValue); 

這裏的strlen(的strstr($密押, 'ITEM_NAME'))如果字符串 'ITEM_NAME' 存在的指數和preg_match_all(檢查 「?/.*(\ d +)$ /」 ,$ testKey,$ matches)檢查如果帶有item_name的字符串以數字值結尾。這將消除所有不以數值結尾的其他item_name鍵。如果你想利用還那麼你可以從狀態刪除preg_macth

希望這有助於。上述

1

爽快的解決方案是太棒了。我只是想補充一點,你實際上有多少產品是你的陣列中的指標這個變量

[num_cart_items] 

可能是,如果你要處理的接線柱陣列有幫助的情況下直接格式化成其他變量,因爲你知道要查找多少物品。

相關問題