循环到数组并只获取特定的键值对

时间:2012-01-28 07:21:20

标签: php

我有这个$ _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] => test1_biz@gmail.com
    [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] => test_biz@gmail.com
    [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] => test_biz@gmail.com
    [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

这些钥匙不是常数,我的意思是,它来自购物车,因此商品名称取决于购买的购物车中放置的商品数量。现在我的问题是,     如何循环到那个post数组,只得到item_names加上它们对应的值?,因为我需要它们并保存在db表中

4 个答案:

答案 0 :(得分:4)

循环遍历数组的每个元素,并使用strposdocs检查密钥是否以“items_name”开头。如果是这样,请将其添加到$items数组。

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

答案 1 :(得分:2)

更优雅的解决方案($ data是你的数组)

  $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
        )

)

答案 2 :(得分:1)

假设$ test作为包含值的输出数组。你可以这样做:

$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($ testKey,'item_name'))检查字符串'item_name'是否作为索引存在而preg_match_all(“/.*?(\ d +)$ /”,$ testKey,$ matches)检查是否item_name的字符串以数字值结尾。这将消除不以数字值结尾的所有其他item_name键。如果你想要那个,那么你可以从条件

中删除preg_macth

希望这有帮助。

答案 3 :(得分:1)

Cheery的解决方案非常棒。我只是想补充一点,你实际上已经知道了这个变量

中阵列中有多少产品
[num_cart_items]

如果您必须直接处理POST ARRAY而不将其重新格式化为其他变量,因为您知道需要查找的项目数量,这可能会有所帮助。