1

どうすれば目的の出力を得ることができますか? array1の「entry_id」がarray2の「entry_id」と同じでないかどうかを確認したい

どうやってやるの?私はarray_diff_keyで試しました..しかし、それはうまくいきませんでした

アレイ1

Array ( [0] => Array ( [entry_id] => 53242 [site_id] => 2 [channel_id] => 2 [author_id] => 5 [1] => Array ( [entry_id] => 76435 [site_id] => 8 [channel_id] => 8 [author_id] => 9  

配列2

Array ( [0] => Array ( [entry_id] => 89764 [site_id] => 1 [channel_id] => 1 [author_id] => 5  [1] => Array ( [entry_id] => 53242[site_id] => 7 [channel_id] => 9 [author_id] => 1  

array1 と array2 を互いに比較して、目的の出力を得たいと思います。

望ましい出力:

   Array ( [0] => Array ( [entry_id] => 76434 [site_id] => 8 [channel_id] => 8 [author_id] => 9 [1] => Array ( [entry_id] => 76435 [site_id] => 8 [channel_id] => 8 [author_id] => 9 
4

2 に答える 2

0

小さいサイズの入力配列の場合、基本的に2つのforループを使用して長い道のりでそれを行うことができます。

サンプルコード(テストされていません!):

// $array1 and $array2 are the inputs
// $array3 is output
$array3=array();
// Keep track of what we have already found
$buff=array();
foreach($array1 as $a1){
    foreach($array2 as $a2){
        // Check for matching ids and is not already used
        if($a1['entry_id']==$a2['entry_id'] and !isset($buff[$a1['entry_id']])){
            $array3[]=$a1;
            $buff[$a1['entry_id']]=1;
        }
    }
}

print_r($array3); // Debug

編集: OPは反対を望んでいたので...

サンプルコード(テスト済み!):

// 2 pass filter
// $array1 and $array2 are the inputs
$array1=array(
    array('entry_id'=>1,'t'=>"a1"),
    array('entry_id'=>2,'t'=>"b1"),
    array('entry_id'=>3,'t'=>"c1"),
);
$array2=array(
    array('entry_id'=>1,'t'=>"a2"),
    array('entry_id'=>2,'t'=>"b2"),
    array('entry_id'=>4,'t'=>"d2"),
);

// $array3 is output
$array3=array();
// Keep track of what we have already found
$buff=array();

foreach($array1 as $a1) $buff[$a1['entry_id']]=array(1,$a1);

foreach($array2 as $a2){
    if(isset($buff[$a2['entry_id']])){
            $buff[$a2['entry_id']]=array(2,$a2);
    }else{
            $buff[$a2['entry_id']]=array(1,$a2);
    }
}

foreach($buff as $k=>$v) if($v[0]==1) $array3[]=$v[1];

print_r($array3); // Debug
于 2013-02-19T12:00:12.293 に答える
0

これを試して:

<?php
$arreglo1 = Array (
        Array (
            "entry_id" => 53242,
            "site_id" => 2,
            "channel_id" => 2,
            "author_id" => 1
        ),
        Array (
            "entry_id" => 76435,
            "site_id" => 8,
            "channel_id" => 8,
            "author_id" => 9,
        )
    );

    $arreglo2 = Array (
        Array (
            "entry_id" => 89764, 
            "site_id" => 1,
            "channel_id" => 1,
            "author_id" => 5
        ),
        Array (
            "entry_id" => 53242,
            "site_id" => 7,
            "channel_id" => 9,
            "author_id" => 1
            )
        );

     $cnt = 0;
     foreach($arreglo1 as $arr1) {
      foreach($arreglo2 as $arr2){
       if ($arr1["entry_id"] == $arr2["entry_id"]) {
         $agregarlo = FALSE;
         break;
       }
       else {
        $agregarlo = TRUE;
       } 
      }

      if ($agregarlo) {
       $resultado[$cnt] = $arr1;
      }
      $cnt++;
     }

     var_dump($resultado);
    ?>

Wichはあなたにこれを与えます:

array(1) { [1]=> array(4) { ["entry_id"]=> int(76435) ["site_id"]=> int(8) ["channel_id"]=> int(8) ["author_id"]=> int(9) } }
于 2013-02-19T17:07:09.290 に答える