dravpuso44681 2014-05-19 05:33
浏览 54

如何在不使用PHP中的循环结构的情况下从多维动态数组的内部数组中获取值?

I've a following dynamic multi-dimensional array titled $rebate_by_product_data:

Array
(
    [0] => Array
        (
            [id] => 93
            [start_date] => 2014-06-01
            [end_date] => 2014-06-30
            [count] => 10000
            [rebate_type_id] => 2
            [created_at] => 1400148462
            [updated_at] => 1400148462
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [id] => 446
                            [state_id] => 2
                            [rebate_id] => 93
                            [created_at] => 1400148462
                            [updated_at] => 1400148462
                            [state_name] => Alaska
                        )

                    [1] => Array
                        (
                            [id] => 447
                            [state_id] => 14
                            [rebate_id] => 93
                            [created_at] => 1400148462
                            [updated_at] => 1400148462
                            [state_name] => Indiana
                        )

                    [2] => Array
                        (
                            [id] => 448
                            [state_id] => 23
                            [rebate_id] => 93
                            [created_at] => 1400148462
                            [updated_at] => 1400148462
                            [state_name] => Minnesota
                        )

                    [3] => Array
                        (
                            [id] => 449
                            [state_id] => 35
                            [rebate_id] => 93
                            [created_at] => 1400148462
                            [updated_at] => 1400148462
                            [state_name] => Ohio
                        )

                    [4] => Array
                        (
                            [id] => 450
                            [state_id] => 42
                            [rebate_id] => 93
                            [created_at] => 1400148462
                            [updated_at] => 1400148462
                            [state_name] => Tennessee
                        )

                )

            [rebate_product] => Array
                (
                    [0] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 13
                                            [product_name] => Budwiser
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 16
                                            [product_name] => Fosters
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 169
                                    [quantity] => 20
                                    [pack_of] => 10
                                    [volume] => 30
                                    [volume_unit_id] => 9
                                    [amount] => 40
                                    [created_at] => 1400148462
                                    [updated_at] => 1400148462
                                )

                        )

                    [1] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 14
                                            [product_name] => Kingfisher Strong
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 15
                                            [product_name] => Tuborg
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 170
                                    [quantity] => 60
                                    [pack_of] => 50
                                    [volume] => 70
                                    [volume_unit_id] => 20
                                    [amount] => 80
                                    [created_at] => 1400148462
                                    [updated_at] => 1400148462
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 99
            [start_date] => 2014-05-29
            [end_date] => 2014-05-30
            [count] => 12
            [rebate_type_id] => 2
            [created_at] => 1400163679
            [updated_at] => 1400248387
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [id] => 500
                            [state_id] => 1
                            [rebate_id] => 99
                            [created_at] => 1400248387
                            [updated_at] => 1400248387
                            [state_name] => Alabama
                        )

                    [1] => Array
                        (
                            [id] => 501
                            [state_id] => 12
                            [rebate_id] => 99
                            [created_at] => 1400248387
                            [updated_at] => 1400248387
                            [state_name] => Idaho
                        )

                    [2] => Array
                        (
                            [id] => 502
                            [state_id] => 14
                            [rebate_id] => 99
                            [created_at] => 1400248387
                            [updated_at] => 1400248387
                            [state_name] => Indiana
                        )

                    [3] => Array
                        (
                            [id] => 503
                            [state_id] => 27
                            [rebate_id] => 99
                            [created_at] => 1400248387
                            [updated_at] => 1400248387
                            [state_name] => Nebraska
                        )

                )

            [rebate_product] => Array
                (
                    [0] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 12
                                            [product_name] => Signature Scotch Whiskey
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 13
                                            [product_name] => Budwiser
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                    [2] => Array
                                        (
                                            [product_id] => 15
                                            [product_name] => Tuborg
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 195
                                    [quantity] => 10
                                    [pack_of] => 12
                                    [volume] => 100
                                    [volume_unit_id] => 7
                                    [amount] => 200
                                    [created_at] => 1400248388
                                    [updated_at] => 1400248388
                                )

                        )

                    [1] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 16
                                            [product_name] => Fosters
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 14
                                            [product_name] => Kingfisher Strong
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 196
                                    [quantity] => 2
                                    [pack_of] => 12
                                    [volume] => 200
                                    [volume_unit_id] => 9
                                    [amount] => 77777
                                    [created_at] => 1400248388
                                    [updated_at] => 1400248388
                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 100
            [start_date] => 2014-06-26
            [end_date] => 2014-06-30
            [count] => 10000
            [rebate_type_id] => 2
            [created_at] => 1400240035
            [updated_at] => 1400243964
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [id] => 495
                            [state_id] => 2
                            [rebate_id] => 100
                            [created_at] => 1400247125
                            [updated_at] => 1400247125
                            [state_name] => Alaska
                        )

                    [1] => Array
                        (
                            [id] => 496
                            [state_id] => 14
                            [rebate_id] => 100
                            [created_at] => 1400247125
                            [updated_at] => 1400247125
                            [state_name] => Indiana
                        )

                    [2] => Array
                        (
                            [id] => 497
                            [state_id] => 23
                            [rebate_id] => 100
                            [created_at] => 1400247125
                            [updated_at] => 1400247125
                            [state_name] => Minnesota
                        )

                    [3] => Array
                        (
                            [id] => 498
                            [state_id] => 35
                            [rebate_id] => 100
                            [created_at] => 1400247125
                            [updated_at] => 1400247125
                            [state_name] => Ohio
                        )

                    [4] => Array
                        (
                            [id] => 499
                            [state_id] => 42
                            [rebate_id] => 100
                            [created_at] => 1400247125
                            [updated_at] => 1400247125
                            [state_name] => Tennessee
                        )

                )

            [rebate_product] => Array
                (
                    [0] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 13
                                            [product_name] => Budwiser
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 16
                                            [product_name] => Fosters
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 193
                                    [quantity] => 20
                                    [pack_of] => 10
                                    [volume] => 30
                                    [volume_unit_id] => 9
                                    [amount] => 40
                                    [created_at] => 1400247125
                                    [updated_at] => 1400247125
                                )

                        )

                    [1] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 14
                                            [product_name] => Kingfisher Strong
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 15
                                            [product_name] => Tuborg
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 194
                                    [quantity] => 6000
                                    [pack_of] => 500
                                    [volume] => 700
                                    [volume_unit_id] => 9
                                    [amount] => 805
                                    [created_at] => 1400247125
                                    [updated_at] => 1400247125
                                )

                        )

                )

        )

    [3] => Array
        (
            [id] => 102
            [start_date] => 2014-05-20
            [end_date] => 2014-05-31
            [count] => 545
            [rebate_type_id] => 2
            [created_at] => 1400264987
            [updated_at] => 1400264987
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [id] => 505
                            [state_id] => 2
                            [rebate_id] => 102
                            [created_at] => 1400264987
                            [updated_at] => 1400264987
                            [state_name] => Alaska
                        )

                    [1] => Array
                        (
                            [id] => 506
                            [state_id] => 3
                            [rebate_id] => 102
                            [created_at] => 1400264987
                            [updated_at] => 1400264987
                            [state_name] => Arizona
                        )

                )

            [rebate_product] => Array
                (
                    [0] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 12
                                            [product_name] => Signature Scotch Whiskey
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 200
                                    [quantity] => 7
                                    [pack_of] => 10
                                    [volume] => 88
                                    [volume_unit_id] => 7
                                    [amount] => 56
                                    [created_at] => 1400264987
                                    [updated_at] => 1400264987
                                )

                        )

                )

        )

    [4] => Array
        (
            [id] => 103
            [start_date] => 2014-05-07
            [end_date] => 2014-05-29
            [count] => 12456
            [rebate_type_id] => 2
            [created_at] => 1400266051
            [updated_at] => 1400266051
            [applicable_states] => Array
                (
                    [0] => Array
                        (
                            [id] => 507
                            [state_id] => 3
                            [rebate_id] => 103
                            [created_at] => 1400266051
                            [updated_at] => 1400266051
                            [state_name] => Arizona
                        )

                    [1] => Array
                        (
                            [id] => 508
                            [state_id] => 5
                            [rebate_id] => 103
                            [created_at] => 1400266051
                            [updated_at] => 1400266051
                            [state_name] => California
                        )

                )

            [rebate_product] => Array
                (
                    [0] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 12
                                            [product_name] => Signature Scotch Whiskey
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 16
                                            [product_name] => Fosters
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 1
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 201
                                    [quantity] => 1
                                    [pack_of] => 
                                    [volume] => 1
                                    [volume_unit_id] => 10
                                    [amount] => 4
                                    [created_at] => 1400266051
                                    [updated_at] => 1400266051
                                )

                        )

                    [1] => Array
                        (
                            [products] => Array
                                (
                                    [0] => Array
                                        (
                                            [product_id] => 13
                                            [product_name] => Budwiser
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                    [1] => Array
                                        (
                                            [product_id] => 15
                                            [product_name] => Tuborg
                                            [manufacturer_id] => 37
                                            [company_name] => Amazon
                                            [rebate_variant_id] => 2
                                        )

                                )

                            [saleable_unit] => Array
                                (
                                    [id] => 202
                                    [quantity] => 1
                                    [pack_of] => 
                                    [volume] => 1
                                    [volume_unit_id] => 10
                                    [amount] => 5
                                    [created_at] => 1400266051
                                    [updated_at] => 1400266051
                                )

                        )

                )

        )

)

You can see from above that the actual array can be very large in size of very small too. It depends on the data entered by user. Now if you observe this array closely in each inner array with key [products] there are more arrays. But in each of these inner arrays present under [products] key the key [manufacturer_id] is having the same value associated with it 37.

I want to assign this value to a new variable, that's it. I don't want any of the data from the above array. So I think to get the single value from this very big array it will not a good practice to apply many loops. I want to get this value optimally in minimal no. of code lines. How should I achieve this?

Can someone please help me in this regard please?

  • 写回答

1条回答 默认 最新

  • douzhong4222 2014-05-19 06:16
    关注

    If you decide to loop. I know you said you didn't want to loop, but 3 loops isn't so many. So I did it for you anyway, case you decide to change your mind.

    //Loop through your array
    foreach($rebate_by_product_data AS $key => $product_data){
        //Check if the rebate_product instance has been set
        if(!isset($product_data['rebate_product'])){
            //If it hasn't skip this loop continue onto the next
            continue;
        }
        //Loop through the rebate_products
        foreach($product_data['rebate_product'] AS $products){
            //rebate_products should all have manufacturer_id's ?
            foreach($products AS $product){
                //Get your manufacturer ID.
                $manufacturer_id = $product['manufacturer_id'];
            }
        }
    }
    

    If you really don't want to loop.

    //Second way is to just call the indexes off one by one, statically, if you only need one instance of the manufacturer_id
    $manufacturer_id = $rebate_by_product_data[0]['rebate_product'][0]['products'][0]['manufacturer_id'];
    

    Bare in mind I didn't re-create your array. So there might be a mistake. Will adjust the loop, or the static call if needed.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置