0

次のように、ある配列のキーを削除する必要があります。別の配列にはありません

    /**
     * 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
     */

しかし、配列の日付フィールドを返すと、配列から $Fields にないキーの設定を解除する必要があります。配列に名前がない場合は、LastName のみを返します。

[]さん、ありがとうございます

4

1 に答える 1

0

適切な英語で質問を構成することを検討する必要があります。主な理由は、ユーザーが回答せずに批判するか、完全に無視するためです。

そうは言っても、コンマで区切られたインデックスを持つ配列と文字列があると思います。次に、余分なデータを削除して配列を「サニタイズ」します。

これを行う方法の例を次に示します。

<?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);
?>
于 2011-04-23T18:44:47.737 に答える