将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

php remove the value from array
php从 array 中删除值

need help replacing array value i`m trying check experience and space exists in the below array and replace space with experience max (i,e next array values if its equal null/space replace with experience max), i have pasted code below which im using to find and replace array value, its replacing the experience to experience max instead of empty to experience max


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

时间: 作者:

Ok, here is the solution explained :


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

Useful links : foreach, key, empty.Hope it will help! :- )

原作者:
...