Array sentenced to heavy and update duplicate values

One-dimensional array:

$arr = [1,2,3,4,5,4,3,2,1,1,1,1];
updateRepeat($arr);

 

process result:

array(12) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> string(4) "4(1)" [6]=> string(4) "3(1)" [7]=> string(4) "2(1)" [8]=> string(4) "1(1)" [9]=> string(4) "1(2)" [10]=> string(4) "1(3)" [11]=> string(4) "1(4)" }

 

Two-dimensional array:

   $arr = [
            ['name' =>'233','sex' => 1],
            ['name' =>'456'],
            ['name' =>'hahaha'],
            ['name' =>'qwe'],
            ['name' =>'965'],
            ['name' =>'2333'],
            ['name' =>'233','num' => 2],
            ['name' =>'hahaha'],
            ['name' =>'233','sex' => 1],
            ['name' =>'233','sex' => 1],
            ['name' =>'233','sex' => 1],
            ['name' =>'233','sex' => 1]
     ];
  
updateRepeat($arr,'name');

 

process result:

array(12) { [0]=> array(2) { ["name"]=> string(3) "233" ["sex"]=> int(1) } [1]=> array(1) { ["name"]=> string(3) "456" } [2]=> array(1) { ["name"]=> string(6) "hahaha" } [3]=> array(1) { ["name"]=> string(3) "qwe" } [4]=> array(1) { ["name"]=> string(3) "965" } [5]=> array(1) { ["name"]=> string(4) "2333" } [6]=> array(2) { ["name"]=> string(6) "233(1)" ["num"]=> int(2) } [7]=> array(1) { ["name"]=> string(9) "hahaha(1)" } [8]=> array(2) { ["name"]=> string(6) "233(2)" ["sex"]=> int(1) } [9]=> array(2) { ["name"]=> string(6) "233(3)" ["sex"]=> int(1) } [10]=> array(2) { ["name"]=> string(6) "233(4)" ["sex"]=> int(1) } [11]=> array(2) { ["name"]=> string(6) "233(5)" ["sex"]=> int(1) } }

 

Code:

    // two-dimensional array of the specified field, to change the value of the duplicate 
    public  function updateRepeat ( $ ARR , $ Field ) 
    { 
        $ newarr = Array ();
         the foreach ( $ ARR  AS  $ K => $ V ) 
        { 
            IF ( the in_array ( $ V [ Field $ ], $ newarr )) 
            { 
                $ newValue = $ the this -> updateField ( $ newarr , $ V [ $ Field ], $ V [ $ Field]);
                 // the value is repeated to update the value of modification of the original array 
                $ ARR [ $ K ] [ $ Field ] = $ newValue ;
                 // the value of the repeated values into modified heavy sentence array 
                $ newarr [ $ K ] = $ newValue ; 
            } the else {
                 // will not be repeated determination of the weight values into the array 
                $ newarr [ $ K ] = $ V [ $ Field ]; 
            } 
        } 
        return  $ ARR ; 
    } 

    // one-dimensional array of repeated changes value 
    public  function updateRepeat1 ($ ARR ) 
    { 
        $ newarr = Array ();
         the foreach ( $ ARR  AS  $ K => $ V ) 
        { 
            IF ( the in_array ( $ V , $ newarr )) 
            { 
                $ newValue = $ the this -> updateField ( $ newarr , $ V , $ V );
                 // the value is repeated to update the value of modification of the original array 
                $ ARR [ $ K ] = $ newValue ;
                 // the value of the repeated values into modified heavy sentence array 
                $ newarr[ $ K ] = $ newValue ; 
            } the else {
                 // will not be repeated determination of the weight values into the array 
                $ newarr [ $ K ] = $ V ; 
            } 
        } 
        return  $ ARR ; 
    } 
    // repeats the process value 
    public  function updateField ( $ ARR , $ oldv , $ newv , $ NUM =. 1 ) 
    { 
        // to achieve the recursive re-judgment so heavy 
        IF ( the in_array ( $ newv , $ ARR , to true))
        {
            $newvalue = $oldv.'('.$num.')';
            $newnum = $num + 1;
            $res = $this->updateField($arr,$oldv,$newvalue,$newnum);
        } else {
            return $newv;
        }
        return $res;
    }

A simple array weight determination, the principle is to traverse the array, sequentially retrieves values, each takes on a value and a set of values ​​taken prior to matching, if there are repeated to update the value.

Note in_array () third argument, defaults to false == match, true use === match, if you do not use exactly match, an error will determine the value of an integer.

Guess you like

Origin www.cnblogs.com/yuanshen/p/12082994.html