在两个数组之间设置new / remove / changed key =>值对

时间:2016-08-11 18:05:50

标签: php arrays

我有两个名为$ old和$ new的多维数组。我想比较两个数组并添加新的key =>数组中的值new $ new k => v' s在两者之间添加新内容,删除或更改。

这些是数组:

$old = array(
  array('id'=> 111,'color'=> 'blue'),
  array('id'=> 112,'color'=> 'red'),
  array('id'=> 113,'color'=> 'black'),
  array('id'=> 114,'color'=> 'smoke')
);

$new = array(
      array('id'=> 111,'color'=> 'green'),
      array('id'=> 112,'color'=> 'red'),
      array('id'=> 113,'color'=> ''),
      array('color'=> 'new color')
    );

我想检查2数组new,old并返回新数组$ end_result。

$end_result  = array(
      array('id'=> 111,'color'=> 'green','action'=>'changed'),
      array('id'=> 112,'color'=> 'red','action'=>''),
      array('id'=> 113,'color'=> '','action'=>'remove'),
      array('color'=> 'new color','action'=>'new'),
      array('id'=> 114,'color'=> 'smoke','action'=>'remove')
    );

我写道:

$newarray = array();
foreach ($new as $key_new => $value_new) {
    $newarray[$key_new] = $value_new;
    if(!isset($value_new['id'])){
        $newarray[$key_new]['action'] = 'new';
    }else {        
        foreach ($old as $key_old => $value_old) {            
            if($value_new['id'] == $value_old['id']){
                if(empty($value_new['color'])){                    
                    $newarray[$key_new]['action'] = 'remove';
                }else {
                    if($value_new['color'] != $value_old['color']){
                        $newarray[$key_new]['action'] = 'changed';
                    }
                }

            }

        }
    }

}
echo json_encode($newarray);

//result: [{"id":111,"color":"green","action":"changed"},{"id":112,"color":"red"},{"id":113,"color":"","action":"remove"},{"color":"new color","action":"new"}]

但是!如何在数组old中检查Key id 114,并设置action" remove"在$ newarray

谢谢!

1 个答案:

答案 0 :(得分:0)

这是解决方案的代码,您应该尝试一下。

$old = array(
    array('id'=> 111,'color'=> 'blue'),
    array('id'=> 112,'color'=> 'red'),
    array('id'=> 113,'color'=> 'black'),
    array('id'=> 114,'color'=> 'smoke')
);

$new = array(
    array('id'=> 111,'color'=> 'green'),
    array('id'=> 112,'color'=> 'red'),
    array('id'=> 113,'color'=> ''),
    array('color'=> 'new color')
);

// Set the output array to the new array, which is the gradually compared to the old one
$array = $new;
foreach ($old as $old_key => $oldarray)
{
    // Flag is to check if the old id had any match in the new, else it is removed
    $flag = true;
    foreach ($new as $new_key => $newarray)
    {
        if (empty($newarray['id']))
        {
            $array[$new_key]['action'] = 'new';
        }
        else if ($newarray['id'] == $oldarray['id'])
        {
            $flag = false;
            if (empty($newarray['color']))
            {
                $array[$new_key]['action'] = 'remove';
            }
            else if ($newarray['color'] != $oldarray['color'])
            {
                $array[$new_key]['action'] = 'change';
            }
        }
    }
    if ($flag)
    {
        // Make sure we don't override any index already existing
        $key = 0;
        while (array_key_exists($key, $array))
        {
            $key++;
        }
        $array[$key] = $oldarray;
        $array[$key]['action'] = 'remove';
    }
}

echo json_encode($array);

// Result: [{"id":111,"color":"green","action":"change"},{"id":112,"color":"red"},{"id":113,"color":"","action":"remove"},{"color":"new color","action":"new"},{"id":114,"color":"smoke","action":"remove"}]