检查字符串数组raw是否是最后一个

时间:2017-08-14 17:19:54

标签: php arrays

有谁知道如何检查我的字符串raw是否是最后一个我尝试使用end(...)但是我坚持如何检查字符串数组raw是否是数组中的最后一个

$array = array('raw1' => 'value1', 'raw2' => 'value2', 'raw3' => 'value3');
for($array as $key => $value) {
   if(strcmp(end($array), $key) == False) { // some code which will be excuted when the check returns true  } 
}

我不确定我的例子是否正确,但请任何人帮我检查一下原子是否是最后一个?请

2 个答案:

答案 0 :(得分:0)

您可以使用end()

$array = array('raw1' => 'value1', 'raw2' => 'value2', 'raw3' => 'value3');
$lastElement = end($array);
foreach($array as $k => $v) {
    if($v == $lastElement) {
        // some code which will be executed its last element of an array  
    }
}

答案 1 :(得分:0)

尝试这样的事情:

$array = array('raw1' => 'value1', 'raw2' => 'value2', 'raw3' => 'value3');
$arrayCount = count($array);
$i = 0;
foreach ($array as $key => $value) {
    $i++;
    if ($arrayCount == $i) {
        // some code which will be excuted
    }
}