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

如何使用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'];
    }
    
    点赞 评论
  • dtkago3208 2019-02-27 07:49

    You can use array_search on the week_startDate column (extracted using array_column) to find the key for that value, and then use that key to return the week_endDate:

    $startDate = '2019-02-01';
    $k = array_search($startDate, array_column($array, 'week_startDate'));
    if ($k !== false) {
        $endDate = $array[$k]['week_endDate'];
    }
    else {
        echo "$startDate not found!";
    }
    

    Demo on 3v4l.org

    点赞 评论
  • dou44481 2019-02-27 07:50

    Yes you can do something like this. But not sure which one you want. So you can check this:

    foreach ($array as $arr) {
        if(array_key_exists('week_startDate', $arr)){
            return $arr["week_endDate"];
        }
    }
    

    or you can check this:

    foreach ($array as $arr) {
        if($arr["week_startDate"] == "2019-02-01"){
            return $arr["week_endDate"];
        }
    }
    
    点赞 评论
  • doukui2011 2019-02-27 07:55

    Here you go

    $search_on_me = array(
        array(
            'week_startDate' => '2019-02-01',
            'week_endDate' => '2019-02-03'
        ),
    
        array(
            'week_startDate' => '2019-02-04',
            'week_endDate' => '2019-02-10'
        ),
    
        array(
            'week_startDate' => '2019-02-11',
            'week_endDate' => '2019-02-17'
        )
    );
    
    $key = array_search('2019-02-01', array_column($search_on_me, 'week_startDate'));
    
    echo "week_endDate of 2019-02-01: ".$search_on_me[$key]['week_endDate'];
    
    点赞 评论
  • doujin4031 2019-02-27 07:57
    $array = [
        [
            'week_startDate' => '2019-02-01',
            'week_endDate' => '2019-02-03'
        ], [
            'week_startDate' => '2019-02-04',
            'week_endDate' => '2019-02-10'
        ], [
            'week_startDate' => '2019-02-11',
            'week_endDate' => '2019-02-17'
        ], [
            'week_startDate' => '2019-02-18',
            'week_endDate' => '2019-02-24'
        ], [
            'week_startDate' => '2019-02-25',
            'week_endDate' => '2019-02-28'
        ]
    ];
    

    To get the key:

    $key = array_search('2019-02-01', array_column($array, 'week_startDate'));
    

    To get the value:

    $value = $array[$key]['week_endDate'];
    
    点赞 评论
  • doouzlrvb01417498 2019-02-27 07:57

    You can get an array of the start dates with array_column.

    $startDates = array_column($your_array, 'week_startDate');
    

    The key for the item containing your $needle date can be found with array_search.

    $key = array_search($needle, $startDates);
    

    The return value of array_search can be false, if the $needle is not found. Make sure to check if it's 0 or false!

    if ($key !== false) {
     $endDate = $your_array[$key]['week_endDate'];
    }
    
    点赞 评论
  • doucang2831 2019-02-27 08:06

    I'm using array pointer for pointing every element

    reset($myarray);
    while ($curr = current($myarray))
    {
    
      $key = key($myarray); // Get key of pointing array
      if ( ( $curr['week_startDate'] == $curr['week_endDate'] ) && $curr['week_startDate'] !== '2019-02-01' && $curr['week_startDate'] !== '2019-02-18' )
        print_r($curr);
    
      next($myarray);
    }
    
    点赞 评论

相关推荐 更多相似问题