2012-01-21 126 views
1

我下面,我希望能够基于特定标准来搜索一个特定的值多维数组:如何从php中的多维数组中获取值?

$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 
       ) 
      ) 
     ) 
    ) 
); 

我想要做的是检索基础上给出的type_of_work和时间表的标准数值用户。

例1: 查询的子阵列 “文本”,给出:

  • type_of_work = “选项1”
  • TIMEFRAME = “within_24_hours”
  • 接着的 “1.00” 应该提取

示例2: 搜索子阵列“非文本”,给出:

  • type_of_work = “2选项”
  • 时间表= “within_24_hours”
  • 随后的 “50.00” 值应提取

我怎样才能做到这一点?

回答

3

访问多维数组与访问一维数组几乎是一样的。

下面是基于什么你问一个例子:

// 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']; 

因此,基于上面的例子中,继续建设你的选择,直到你有尺寸/结果,你想:

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

这将返回1.00

使用相同的方法检索50.00。哈哈!

+0

哈!谢谢一堆!我得到它的工作:) – Johny