Remove Key of One Array of Not Have in Other Array [closed]
I Have to Remove The Key of One Array Of Not Have in Another Array, Like
/**
* I Have This Array, With Keys
* Name, Lastname, Date
*/
$Array = Array( 'name' => 'Mike', 'lastname' => 'Griggs', 'date' => strftime( '%A %c' ) );
/**
* And The Split , Make This One Array
*/
$Fields = 'name, lastname';
foreach( split( ',', str_replace开发者_运维技巧( ' ', NULL, $Fields ) ) as $Index => $Field ):
if(!array_key_exists( $Field, split( ',', str_replace( ' ', NULL, $Fields )))):
unset( $Array[$Field] );
endif;
endforeach;
print_r( $Array );
/**
* i Have to Remove The Elements of $Array
* That Not Have in $Fields, In This Case, Unset 'date' From $array
*/
But Retorning The Date Field in Array I Need to Unset The Keys Of Not Have in $Fields From Array, If Dont Have Name in Array, Return Only LastName ..
Thanks []'s
You should consider formating your questions in proper English. Mostly because users will either criticize it instead of answering it or ignore it completely.
That being said, I assume you have an array and a string with comma separated indexes. Then you want to "sanitize" your array by removing extra data.
Here's an example on how you might do that:
<?php
$array = Array( 'name' => 'Mike', 'lastname' => 'Griggs', 'date' => strftime( '%A %c' ) );
$fields = 'name, lastname';
function removeIndex($a,$f){
$f=explode(',',$f);
$b=array();
foreach($f as $v){
$v=trim($v);// only need if you have extra whitespace
$b[$v]=$a[$v];
}
return $b;
}
$array=removeIndex($array,$fields);
print_r($array);
?>
精彩评论