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

如何使用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

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

我的数组是 strong> p>

  数组
(
 [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 
)
 
)
  code>  pre> 
 
 

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

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

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

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

$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'];
}

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

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"];
    }
}

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

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

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

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