doufei9946 2011-02-21 10:40
浏览 197
已采纳

如何将两个数组组成一个数组对象?

I am have two arrays $A(array of object) and $B

  $A = 
  Array
  (
  [0] => objval Object
      (

      [id_groupe] => 51
      )

  [1] => objval Object
      (

      [id_groupe] => 46
      )

  [2] => objval Object
      (
      [id_groupe] => 52
      )

  )


  $B = 
  Array(51,46)

I want to return new one , if it's id_groupe of $A exisiting in $B so it will be expected result like this:

  Array
  (

  [0] => objval Object
      (
      [id_groupe] => 52
      )

  )

Any could help me?

  • 写回答

1条回答 默认 最新

  • duanchu9914 2011-02-21 10:50
    关注

    Ok, this will solve your problem:

    // object class
    class xy{
    
        public $id_groupe = 0;
    
        function xy($id_groupe){
            $this->id_groupe = $id_groupe;
        }
    
    
    }
    
    // initialize test case Array A
    $A = array();
    $A[] = new xy(51);
    $A[] = new xy(46);
    $A[] = new xy(52);
    
    // initialize test case Array B
    $B = array(46,51);
    
    // init result array
    $diff = array();
    
    // Loop through all elements of the first array
    foreach($A as $elem)
    {
      // Loop through all elements of the second loop
      // If any matches to the current element are found,
      // they skip that element
      foreach($B as $elem2)
      {
        if($elem->id_groupe == $elem2) continue 2;
      }
      // If no matches were found, append it to $diff
      $diff[] = $elem;
    }
    
    // test Array
    print_r($diff);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退