php - php从 array 中删除值

  显示原文与译文双语对照的内容
0 0

需要帮助替换 array 值i`m尝试检查的经验和空间存在于 array 和替换空间中,使用 max ( i,如果下一个 array 值等于 null/空格,则替换为经验最大值),我粘贴了下面的代码,以找到和替换 array 值,而不是为体验最大的体验


array[1] => name,
array[2] => Work Experience,
array[3] =>, 
array[4] => company,
array[5] => location

o/p:


array[1] => name,
array[2] => Work Experience,
array[3] => Work Experience Max,
array[4] => company,
array[5] => location

$search ="Work Experience";
foreach ($val as $key=> $cnt) {
//echo"inside foreach".$cnt;
 if ($cnt == $search) {
 echo $keyvalue = $key;
 break;
 }
}
if (isset($keyvalue)) {
 $firstarryval = array_splice($val, $keyvalue, 1);
}

if ($firstarryval == '') {
 array_splice($val, $keyvalue, 0,"Work Experience Max");
} else {
 array_splice($val, $keyvalue, 0,"Work Experience Max");
}

时间: 原作者:

0 0

好的,下面是解释的问题:


$arr = array( 0=>'name', 1=>'Work Experience', 2=>'', 3=>'company', 4=>'location' );
var_export($arr); echo '<br/>';
$search ="Work Experience";
$arrkey = false;
foreach($arr as $key=>$value){//set variables and advance internal pointer
 echo"inside foreach $key => $value <br/>";
 if( $value == $search ) {
 echo $key;
 echo $arrkey = key($arr);//internal php pointer already points to next array key
 break;
 }
}
if ($arrkey!=false && empty($arr[$arrkey])) {
 $arr[$arrkey] = 'Work Experience Max';
}
echo '<br/>'; var_export($arr);

有用的链接: foreach, 关键 。 希望它会有帮助- ) !

原作者:
...