dongying3830 2012-09-11 23:48
浏览 57
已采纳

Array_udiff对象数组使得数组包含的对象只存在一个特定方法的实例

I need to transorm an array of objects so that for instance, the array will only contain 1 object where the object property 'bar' = b.

So I'd like to turn:

   Array ( [0] => stdClass Object ( 
    [id] = 1
    [foo] => 'a' 
    [bar] => 'b' )

    [1] => stdClass Object ( 
    [id] => 2
    [foo] => 'a' 
    [bar] => 'c' )

    [2] => stdClass Object ( 
    [id] => 3
    [foo] => 'y' 
    [bar] => 'b' )  )

into:

     Array ( [0] => stdClass Object ( 
    [id] = 1
    [foo] => 'a' 
    [bar] => 'b' )

    [1] => stdClass Object ( 
    [id] => 2
    [foo] => 'a' 
    [bar] => 'c' )

However, I need this to work on a larger scale, so I am stumped on how to combine array_filter, array_udiff and array_slice to get what I want. I was thinking that array_filter would get me all the keys where 'foo' = 'b'. Then I could take this array and array_udiff it against the original to get the items where 'foo'=! 'b'. Then I could slice the matches from the array_filter to get a single-key array... and merge that back with the array_udiff difference.

$matches = array_filter($array, "bar_filter");

$remainder = array_udiff($array, $matches, 'compare_objects');

$single =  array_slice( $matches, 0, 1);

$result = array_merge($single, $remainder);

function foobar_filter($obj) {
    if ( $obj->bar == 'b' ) {
        return true;
    } else {
        return false;
    }
}

function compare_objects($obj_a, $obj_b) {
  return $obj_a->id = $obj_b->id;
}

My compare_objects function is off for sure, but I don't know what to put there. Would it be better to just run a second array_filter to get the non-matching items? I need to split the array into keys where 'bar'='b' and where 'bar'!='b' and merge it back together with only 1 object where 'bar'='b'

  • 写回答

1条回答 默认 最新

  • dongyue3795 2012-09-12 00:51
    关注

    Ok, so for clarity, I'll do it in one loop.

    $matches = array();
    $remainder = array();
    
    // $index if you need to maintain index=>object relation
    foreach($array as $index => $object) {
      if($object->bar == "b")
        $matches[] = $object;
      else
        $remainder[] = $object;
    }
    

    -- for loop

    for($index=0,$count=count($array);$index<$count;$index++) {
      if($object->bar == "b")
        $matches[] = $array[$index];
      else
        $remainder[] = $array[$index];
    }
    

    --

    then you would like to merge the result with the first index in $matches

    $result = array_merge(array($matches[0]),$remainder);
    

    If this is the case, then you just need to change the compare object body, which should do the trick in filtering the remaining objects out

    return $obj_a->id == $obj_b->id ? 0 : 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值