PHP二维数组和foreach代码迭代

时间:2014-03-12 16:53:52

标签: php multidimensional-array foreach

我需要在二维数组中迭代一些代码。在我的表单中有一个输入文本框,用户可以在其中插入日期

数组$ myArray是

Array
(
    [range1] => Array
        (
            [From] => 01/07/2002
            [To] => 31/12/2002
            [Rate] => 10.35
        )

    [range2] => Array
        (
            [From] => 01/01/2003
            [To] => 30/06/2003
            [Rate] => 9.85
        )

    [range3] => Array
        (
            [From] => 01/07/2003
            [To] => 31/12/2003
            [Rate] => 9.1
        )
    .
    .
    .
    .
    [range24] => Array
        (
            [From] => 01/01/2014
            [To] => 30/06/2014
            [Rate] => 8.25
        )

)

我想检查每个范围(从range1到range24),如果用户插入的日期小于[From]或grather而不是[To]或落在范围内。 类似的东西:

if ($dateInserted >= $myArray[range1][From]) {

        ...do something...

    } elseif ($dateInserted < $myArray[range1][To]) {

            ...do something...

        } else {

            ...do something...

        }

           and so on foreach range

提前致谢

1 个答案:

答案 0 :(得分:1)

foreach ($myArray as $ranges) {
    foreach ($ranges as $data) {

       if ($dateInserted >= $data['From']) {

           #Do Stuff

       } else {


       }

   }
}

你明白了......