简体   繁体   English

如何从PHP 2个数组中选择不同的键和值并更改相同的值?

[英]how to pick different keys and values from 2 arrays in php and alter the same?

I have two array like 我有两个像

previous: 以前:

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

current: 当前:

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

Now i have to compare these two array and want to alter the changed current array key or values and wrap the elements like 现在,我必须比较这两个数组,并想要更改更改后的当前数组键或值,并像这样包装元素

Array(
        [name] => [<span class='bold_view'> ud </span>]
        [<span class='bold_view'> state </span>] => [anand]
)
$current['name']="<span class='bold_view'> ".$current['name']." </span>";
$current['<span class='bold_view'> state </span>']=$current['state'];

I have to say that it doesn't make much sense but here it is.. 我不得不说,这没有多大意义,但是在这里。

Copy this code and just execute : and see the view source (Ctrl+u) 复制此代码,然后执行:并查看视图源(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);

?>
$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);  

I have done like this and i got perfect answer 我已经做到了,我得到了完美的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM