如何从PHP中的多维数组中获取价值?

时间:2022-04-30 09:58:35

I the following multidimensional array that I want to be able to search a particular value based on certain criteria:

我希望以下多维数组能够根据特定条件搜索特定值:

$pay_rate_lookup = array(
    "text" => array( //rates for text files
        "type_of_work" => array(
            "option1" => array(
                "timeFrame" => array(
                    "within_24_hours" => 1.00,
                    "within_1_2_days" => 2.00,
                    "within_3_5_days" => 3.00,
                    "within_1_2_weeks" => 4.00
                )
            ),
            "option2" => array(
                "timeFrame" => array(
                    "within_24_hours" => 5.00,
                    "within_1_2_days" => 3.00,
                    "within_3_5_days" => 2.00,
                    "within_1_2_weeks" => 2.00
                )
            ),
            "option3" => array(
                "timeFrame" => array(
                    "within_24_hours" => 5.00,
                    "within_1_2_days" => 5.00,
                    "within_3_5_days" => 4.00,
                    "within_1_2_weeks" => 2.00
                )
            ),
            "option4" => array(
                "timeFrame" => array(
                    "within_24_hours" => 2.00,
                    "within_1_2_days" => 8.00,
                    "within_3_5_days" => 5.00,
                    "within_1_2_weeks" => 1.00
                )
            )
        )
    ),
    "non-text" => array(
        "type_of_work" => array(
            "option1" => array(
                "timeFrame" => array(
                    "within_24_hours" => 10.00,
                    "within_1_2_days" => 20.00,
                    "within_3_5_days" => 30.00,
                    "within_1_2_weeks" => 40.00
                )
            ),
            "option2" => array(
                "timeFrame" => array(
                    "within_24_hours" => 50.00,
                    "within_1_2_days" => 30.00,
                    "within_3_5_days" => 20.00,
                    "within_1_2_weeks" => 20.00
                )
            ),
            "option3" => array(
                "timeFrame" => array(
                    "within_24_hours" => 50.00,
                    "within_1_2_days" => 50.00,
                    "within_3_5_days" => 40.00,
                    "within_1_2_weeks" => 20.00
                )
            ),
            "option4" => array(
                "timeFrame" => array(
                    "within_24_hours" => 20.00,
                    "within_1_2_days" => 80.00,
                    "within_3_5_days" => 50.00,
                    "within_1_2_weeks" => 10.00
                )
            )
        )
    )
);

What I would like to do is retrieve the numeric value based on the type_of_work and timeFrame criteria given by the user.

我想要做的是根据用户给出的type_of_work和timeFrame标准检索数值。

Example1: search the sub array "text", given:

例1:搜索子数组“text”,给出:

  • type_of_work = "option1"
  • type_of_work =“option1”
  • timeFrame = "within_24_hours"
  • timeFrame =“within_24_hours”
  • Then value of "1.00" should be extracted
  • 然后应该提取“1.00”的值

Example2: search the sub array "non-text", given:

例2:搜索子数组“非文本”,给出:

  • type_of_work = "option3"
  • type_of_work =“option3”
  • timeFrame = "within_24_hours"
  • timeFrame =“within_24_hours”
  • Then value of "50.00" should be extracted
  • 然后应提取“50.00”的值

How can I accomplish this?

我怎么能做到这一点?

1 个解决方案

#1


3  

Accessing a multi-dimensional array is just about the same as accessing a one dimensional array.

访问多维数组与访问一维数组几乎相同。

Here's an example based on what you're asking:

以下是根据您的要求提供的示例:

// this contains the entire "text" dimension.
$pay_rate_lookup['text'];

// contains the entire type_of_work dimension inside the text dimension.
$pay_rate_lookup['text']['type_of_work'];

So based on the above examples, keep building out your select until you have the dimension/results you want:

因此,根据上面的示例,继续构建您的选择,直到您拥有所需的维度/结果:

$pay_rate_lookup['text']['type_of_work']['option1']['timeFrame']['within_24_hours'];

That will return 1.00.

那将返回1.00。

Use the same method to retrieve 50.00.

使用相同的方法检索50.00。

#1


3  

Accessing a multi-dimensional array is just about the same as accessing a one dimensional array.

访问多维数组与访问一维数组几乎相同。

Here's an example based on what you're asking:

以下是根据您的要求提供的示例:

// this contains the entire "text" dimension.
$pay_rate_lookup['text'];

// contains the entire type_of_work dimension inside the text dimension.
$pay_rate_lookup['text']['type_of_work'];

So based on the above examples, keep building out your select until you have the dimension/results you want:

因此,根据上面的示例,继续构建您的选择,直到您拥有所需的维度/结果:

$pay_rate_lookup['text']['type_of_work']['option1']['timeFrame']['within_24_hours'];

That will return 1.00.

那将返回1.00。

Use the same method to retrieve 50.00.

使用相同的方法检索50.00。