1

配列 1配列 2を使用して、フィルター処理された配列 3を作成しようとしています。

配列 1

Array ( 
          [title] => value 
          [title2] => value2 
          [title3] => value3
      )

配列 2

Array ( 
          [0] => Array ( [id] => 20 [title2] => value2 [otherColumn1] => otherValue1)
          [1] => Array ( [id] => 21 [title4] => value4 [otherColumn3] => otherValue3)
      ) 

交差法を適用した後の望ましい結果:

配列 3

Array ( [title2] => value2 )

これまでのところ、配列 1 と 2の構造が一致しないため、結果を達成できません。さまざまな手法を試しましたが、構造が異なるため比較できません。

   if (!empty($data1['array2'][0])) {

                foreach ($data1['array2'] as $key) {

//                    $filtered= array_intersect($array1,$key);
                    //  print_r($key);
                }
                // $filtered= array_intersect($array1,$data1['array2']);// if i use $data1['array2'][0] it filters fine but just one row

                //    print_r($filtered);
            }

どんな助けでも大歓迎です。ありがとうございました。

4

2 に答える 2

3

配列を考えると:

$arr = array('title' => 'value', 'title2' => 'value2', 'title3' => 'value3');
$arr2 = array (
        0 => array ( 'id' => '20', 'title2' => 'value2', 'otherColumn1' => 'otherValue1'),
        1 => array ( 'id' => '21', 'title4' => 'value4', 'otherColumn3' => 'otherValue3'));

これでフィルタリングされた配列を取得できます:

$merged = call_user_func_array('array_merge', $arr2);
$filtered = array_intersect($arr, $merged);

キーに従って交差させたい場合は、代わりにこれを使用できます。

$filtered = array_intersect_key($arr, $merged);
于 2013-08-13T12:31:25.090 に答える
-1

次の方法で構造の違いを取り除くことができます

$arr1 = array (
        0 => array('title' => 'value', 'title2' => 'value2', 'title3' => 'value3'));
$arr2 = array (
        0 => array ( 'id' => '20', 'title2' => 'value2', 'otherColumn1' => 'otherValue1'),
        1 => array ( 'id' => '21', 'title4' => 'value4', 'otherColumn3' => 'otherValue3'));
于 2013-08-13T12:55:39.530 に答える