dspym82000 2016-06-01 17:43 采纳率: 100%
浏览 88
已采纳

搜索php多维数组并检索键值

I have this $order array and want to store the data in a mysql database.

I need to sort out the products ([type] => physical) and put the key values in one mysql table, and put the other types ([type] => discount and [type] => shipping_fee) in two other tables.

[cart] => Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [type] => physical
                    [name] => Apple iPhone
                    [quantity] => 2
                    [reference] => ABC61
                    [tax_amount] => 329
                    [unit_price] => 5490
                )

            [1] => Array
                (
                    [type] => shipping_fee
                    [name] => FedEx Express
                    [quantity] => 1
                    [reference] => SHIPPING
                    [tax_amount] => 58
                    [unit_price] => 290
                )

            [2] => Array
                (
                    [type] => physical
                    [name] => IBM Laptop
                    [quantity] => 1
                    [reference] => XYZ10
                    [tax_amount] => 700
                    [unit_price] => 8000
                )

            [3] => Array
                (
                    [type] => discount
                    [name] => Discount Coupon
                    [quantity] => 1
                    [reference] => DISCOUNT
                    [tax_amount] => 0
                    [unit_price] => -650
                )

        )
)

Something like this, but I don't know how to properly search the array and retrive the sibling key values:

if ([type] == 'physical') {
  get key values for [name] and [quantity] .. insert them into table products
} elseif ([type] == 'shipping_fee') {
  get key values for [name] and [quantity] .. insert them into table shipping
} elseif ([type] == 'discount') {
  get key values for [name] and [quantity] .. insert them into table discounts
}
  • 写回答

2条回答 默认 最新

  • duanjue9889 2016-06-01 17:56
    关注

    Here's one way using a switch to determine table names:

    foreach($order['cart']['items'] as $values) {
        switch($values['type']) {
            case 'physical':
                $table = 'products';
                break;
            case 'shipping_fee':
                $table = 'shipping';
                break;
            case 'discount':
                $table = 'discounts';
                break;
        }    
        $query = "INSERT INTO $table (name, quantity)
                         VALUES ({$values['name']}, {$values['quantity']})";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误