donglu9898 2014-05-07 01:04
浏览 25
已采纳

如何在以下场景中使用foreach循环结构循环关联数组?

I've an array titled $rebate_by_product:

Array
(
    [op] => preview
    [id] => 
    [form_submitted] => yes
    [company_id] => 46
    [1] => Array
        (
            [pack] => 10
            [quantity] => 20
            [volume] => 30
            [units] => 9
            [amount] => 40
            [rebate_start_date] => 2014-05-01
            [rebate_expiry_date] => 2014-05-05
            [applicable_states] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

            [rebate_total_count] => 5000
            [products] => Array
                (
                    [1] => 9
                    [2] => 10
                )

        )

    [2] => Array
        (
            [pack] => 50
            [quantity] => 60
            [volume] => 70
            [units] => 10
            [amount] => 80
            [rebate_start_date] => 2014-05-06
            [rebate_expiry_date] => 2014-05-10
            [applicable_states] => Array
                (
                    [0] => 14
                    [1] => 15
                    [2] => 16
                )

            [rebate_total_count] => 10000
            [products] => Array
                (
                    [1] => 11
                    [2] => 8
                )

        )

    [3] => Array
        (
            [pack] => 100
            [quantity] => 200
            [volume] => 300
            [units] => 7
            [amount] => 400
            [rebate_start_date] => 2014-05-21
            [rebate_expiry_date] => 2014-05-30
            [applicable_states] => Array
                (
                    [0] => 26
                    [1] => 33
                    [2] => 42
                )

            [rebate_total_count] => 9999
            [products] => Array
                (
                    [1] => 9
                    [2] => 8
                )

        )

    [multiselect] => 42
)

You can observe from above array that it has few elements which are not array but it has three such elements which are themselves array and even few of its data elements are also arrays so how to loop over this kind of array using foreach loop?

  • 写回答

2条回答 默认 最新

  • douqi1625 2014-05-07 01:24
    关注

    If you just want to print each one the just use foreach loop. Consider this example:

    $product_keys = array(); // edited
    // loop them, if its an array, loop inside it again
    foreach($rebate_by_product as $index => $element) {
        if(is_array($element)) {
            foreach($element as $key => $value) {
                if(is_array($value)) {
    
                    // EDITED
                    if($key == 'products') {
                        $product_keys = array_merge($product_keys, $value);
                    }
    
                    $value = implode(',', $value);
                    echo "$key => $value <br/>";
                } else {
                    echo "$key => $value <br/>";
                }
            }
        } else {
            echo "$index => $element <br/>";
        }
    }
    
    // if product items has duplicates check here (edited)
    if(count($product_keys) != count(array_unique($product_keys))) {
        echo "<script>alert('This array has duplicate products');</script>";
    } else {
        echo "<script>alert('Products are ok');</script>";
    }
    

    Or if you want, you cant just use iterators on this one:

    $recursive = new RecursiveIteratorIterator(new RecursiveArrayIterator($rebate_by_product));
    foreach($recursive as $key => $value) {
        echo "$key => $value <br/>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单