开发者

Remove Key of One Array of Not Have in Other Array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜