1

これが私のSQLデータベースの列の値だとしましょう:

Result  -      Reference

  1                A1
  2                A2
  3                A3
  4                A4
  5                A5

上記の列を取得すると、次の配列が得られます。

$inputValue = array(1,3,5);  //This is the User Input

$result = array(1,2,3,4,5); //This is the mysql fetched result of 'Results' column

$reference = array('A1','A2','A3','A4','A5'); //This is the fetched result of 'Reference' column

$filteredData = array_intersect($inputValue, $result);

print_r($filteredData);

これを行うとarray_intersect($inputValue, $result);、次の出力が得られます。

Array ( [0] => 1 
    [1] => 3 
    [2] => 5 )

array_intersect の後、対応する参照を別の配列に格納するにはどうすればよいでしょうか? つまり、交差の後、 に$filteredDataは配列 values があります1,3,5。ここで、対応する参照を、この値を持つ別の配列に格納することも必要です。$MatchingReference = array(A1,A3,A5);

これはどのように行われますか?

4

3 に答える 3

2

私はこれを試してみます:

$filteredReferences = array();
foreach($filteredData as $key) {
    $filteredReferences[] = $reference[$key-1];
}
于 2014-01-17T22:00:51.160 に答える
1

データベースの 2 つの配列を連想配列に結合します。次に、ユーザー入力を使用して、キーのサブセットのみを選択します。

$filteredData = array_intersect_key(
    array_combine($result, $reference), 
    array_flip($inputValue)
);
于 2014-01-17T22:05:22.757 に答える