dongzhanlu0658 2011-04-29 20:14
浏览 15
已采纳

too long

Need to match 2 keys in 2 multidimensional arrays and return matches from the first array if found.

array1 =>

$arr[1] = array('fruit' => 'apple', 'ver' => '1', 'color' => 'blue', 'name' =>'joe');
$arr[2] = array('fruit' => 'peach', 'ver' => '2', 'color' => 'red', 'name' =>'jane');
$arr[3] = array('fruit' => 'apple', 'ver' => '1', 'color' => 'red', 'name' =>'jack');
$arr[4] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'grey', 'name' =>'joe');



array2 =>

$arr[1] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' =>'joe');
$arr[2] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' =>'jane');

I need to match 2 key values, in this example only return the matches in array1 that match array2. The key values for example are the keys fruit and name.

In the above example you can see this match should only return $arr1 and $arr4 for array1 since they match $arr[1] in array2. I need to return the matches only for array1.

This is an example, the real case I do not know the array varibale indicator or the amount ( likely a few hundred in each).

  • 写回答

3条回答 默认 最新

  • doujianmin6527 2011-04-29 20:19
    关注
    $array = (
        0 => array('fruit' => 'apple' etc....
        etc...
    );
    
    $find_these = array('fruit' => 'apple', 'ver' => 4, 'color' => red, etc...);
    
    $fruit = $find_these['apple'];
    $ver = $find_these['ver'];
    
    $found = array();
    
    foreach($array as $key => $subarray) {
       if (($subarray['fruit'] == $fruit) && ($subarray['ver'] == $ver))
           $found[$key] = $subarray;
       }
    }
    

    After this, $found will be a new array containing copies of all the subarrays which have matching fruit/ver fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应