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

时间:2014-05-07 01:04:07

标签: php arrays foreach associative-array key-value

我有一个名为$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
)

你可以从上面的数组中观察到它有很少的元素不是数组但它有三个这样的元素本身是数组,甚至它的数据元素也很少数组,所以如何使用foreach循环遍历这种数组?

2 个答案:

答案 0 :(得分:2)

如果你只想打印每一个,只需使用foreach循环。考虑这个例子:

$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>";
}

或者如果你愿意,你不能在这个上使用iterators

$recursive = new RecursiveIteratorIterator(new RecursiveArrayIterator($rebate_by_product));
foreach($recursive as $key => $value) {
    echo "$key => $value <br/>";
}

答案 1 :(得分:1)

我建议您使用递归方法将数组的所有条目放在同一级别上,然后打印此数组:

function loopArray($inputVal,$inputKey = "") {
    if(is_array($inputVal)) {
        $output = array();
        foreach($inputVal as $key => $value) {
            $output = array_merge($output,loopArray($value,$key));
        }
        return $output;
    } else {
        return array($inputKey => $inputVal);   
    }
}

// Just for presenting:
$yourArray = array(
    "1" => "1",
    array(
        "2.1" => "2.1",
        array(
            "2.2.1" => "2.2.1"
        )
    ),
    "3" => "3",
    array(
        "4.1" => "4.1"
    )
);

$newArray = loopArray($yourArray);
// > array("1" => 1,"2.1" => "2.1","2.2.1" => "2.2.1","3" => "3","4.1" => "4.1")

foreach($newArray as $key => $value) {
    echo $key." => ".$value."<br/>";   
}
// > 1 => 1
// > 2.1 => 2.1
// > 2.2.1 => 2.2.1
// > 3 => 3
// > 4.1 => 4.1