0

我有两个数组

以前的:

Array(
        [name] => [asdfg]
        [city] => [anand]
)    

当前的:

Array(
        [name] => [ud]
        [state] => [anand]
)

现在我必须比较这两个数组并想要更改更改后的当前数组键或值并包装元素

Array(
        [name] => [<span class='bold_view'> ud </span>]
        [<span class='bold_view'> state </span>] => [anand]
)
4

3 回答 3

0

复制此代码并执行 : 并查看查看源代码 (Ctrl+u)

<?php

$prev   = array("name"=>"[asdfg]","city"=>"[anand]");
$curent = array("name"=>"[ud]","state"=>"[anand]");

$res    = array();
foreach($prev as $key=>$val){
   $res[$key]   = $val;
   if(array_key_exists($key,$curent)){
      $res[$key]   = "[<span class='bold_view'> ".$curent[$key]." </span>]";
   }
   if($new_key = array_search($val,$curent)){
      unset($res[$key]);
      $res["<span class='bold_view'> ".$new_key." </span>"]   = $val;
   }
}

print_r($res);

?>
于 2013-02-11T11:18:42.003 回答
0
$current['name']="<span class='bold_view'> ".$current['name']." </span>";
$current['<span class='bold_view'> state </span>']=$current['state'];

我不得不说这没有多大意义,但在这里..

于 2013-02-11T09:55:42.850 回答
0
$arr_pre = array(
                  "name"=>"asdfg",
                  "city"=>"anand",
                  "address" => "anand",
);

$arr_current= array(
                    "name"=>"ud",
                    "state"=>"anand",
                    "address" => "ananda"
);

$result = array_diff_assoc($arr_current,$arr_pre);
    $count_curr = array_count_values($arr_current);
    $count_old  = array_count_values($arr_pre);

    foreach ($result as $key => $value){



        if(!array_key_exists($key,$arr_pre ))
        {
            $key_new = "<b>".$key."</b>";

            if(!in_array($value,$arr_pre))
            {
                $val = "<b>".$value."</b>";
            }

            else if((isset($count_curr[$value]) != isset($count_old[$value])))
            {
                $val = "<b>".$value."</b>";
            }
            else 
            {
                $val = $value;
            }

            unset($arr_current_info[$key]);
        }
        else {
            $key_new = $key;

            if(!in_array($value,$arr_pre))
            {
                $val = "<b>".$value."</b>";
            }

            else if((isset($count_curr[$value]) != isset($count_old[$value])))
            {
                $val = "<b>".$value."</b>";
            }
            else 
            {
                $val = $value;
            }
        }

        $arr_current_info[$key_new]=$val;

    }


    echo "<pre>";
    print_r($arr_current_info);  

我已经这样做了,我得到了完美的答案

于 2013-02-12T07:33:14.897 回答