dongyue9860
2019-02-27 07:43
采纳率: 100%
浏览 2.1k
已采纳

如何使用PHP从数组中获取key的值?

我的数组如下:

Array
(
    [0] => Array
        (
            [week_startDate] => 2019-02-01
            [week_endDate] => 2019-02-03
        )

    [1] => Array
        (
            [week_startDate] => 2019-02-04
            [week_endDate] => 2019-02-10
        )

    [2] => Array
        (
            [week_startDate] => 2019-02-11
            [week_endDate] => 2019-02-17
        )

    [3] => Array
        (
            [week_startDate] => 2019-02-18
            [week_endDate] => 2019-02-24
        )

    [4] => Array
        (
            [week_startDate] => 2019-02-25
            [week_endDate] => 2019-02-28
        )

)

我希望在匹配 week_startDate 时获得 week enddate 的键值。假设 week_startDate 是:

Case1期望值:week startdate 匹配数组中的2019-02-01,然后返回2019-02-03

Case2期望值:week startdate 匹配数组中的2019-02-18,然后返回2019-02-244

可以使用数组函数或其他方式吗?

图片转代码服务由CSDN问答提供 功能建议

我的数组是

  数组
(
 [0] =>数组
(
 [week_startDate] => 2019-02-01 
 [week_endDate] => 2019-02-03 
)
 
 [  1] =>数组
(
 [week_startDate] => 2019-02-04 
 [week_endDate] => 2019-02-10 
)
 
 [2] =>数组\  n(
 [week_startDate] => 2019-02-11 
 [week_endDate] => 2019-02-17 
)
 
 [3] =>数组
(
 [week_startDate]  => 2019-02-18 
 [week_endDate] => 2019-02-24 
)
 
 [4] =>数组
(
 [week_startDate] => 2019-02-  25 
 [week_endDate] => 2019-02-28 
)
 
)
   
 
 

我想在匹配时获取week_endDate的键值 week_startDate。 对应我的week_startDate是

Case1期望值: week_startDate是数组中的2019-02-01匹配然后我要返回2019-02-03 < / p>

case2期望值: week_startDate是数组中的2019-02-18匹配然后我要返回2019-02-24 \ n

可以使用数组函数或任何其他方式吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • douliaotong4944 2019-02-27 07:52
    已采纳
    $expectedDate = '2019-02-11';
    foreach($arr as $key => $val){
      if($expectedDate ==$val['week_startDate']){
       return $val['week_endDate'];
      }
    }
    

    Another solution

    $startDate = '2019-02-01';
    $k = array_search($startDate, array_column($array, 'week_startDate'));
    if ($k !== false) {
       echo $array[$k]['week_endDate'];
    }
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(6条)

相关推荐 更多相似问题