简体   繁体   中英

"Sync" 2D Array with 1D Array By matching Keys - PHP

I have the following two arrays:

A one dimensional array:

$oneDimArray= array( 
            "id" => 1,
            "internal_key" => "TESTKEY_1",
            "CPU" => "intelTest1",
            "GPU" => "nvidiaTest1", 
            "Soundcard" => "AsusTest1"
            "MAC" => "macTest1",
            "IP" => "ipTest1",
            "VLAN" => "vlanTest1"
            );

And a two dimensional array:

$twoDimArray= array( 
                    "id" => 1,
                    "internal_key" => "TESTKEY_2",
                    "nestedArray1" => array (
                                             "CPU" => "intelTest1",
                                             "GPU" => "nvidiaTest2",   
                                             "Soundcard" => "AsusTest1"
          ),

                    "nestedArray2" => array (
                                             "MAC" => "macTest2",
                                             "IP" => "ipTest1",
                                             "VLAN" => "vlanTest1"
         )
 );

I want a function which takes two arrays, one is OneDimensional and the other TwoDimensional. It iterates over every element of the OneDimensional array, and for each of these elements, it also iterates over all elements of the TwoDimensional array. For each element of the TwoDimensional array it compares the key of the currently selected element of the OneDimensional array to the key of the currently selected element of the TwoDimensional array. Should these keys match, the function writes the value of the currently selected element of the OneDimensional Array to the currently selected element of the TwoDimensional array.

For the endresult, the TwoDimensional Array should be preserved (its "instance", so to say) but have its elements updated accordingly with the data coming from the OneDimensional Array.

EDIT: I found the answer myself, could you please reopen the question so I can post it? :)

I made it! Here is the function necessary to do the job:

  public function recursiveUpdateArray($b1, &$b2, $b1val = null){
    /**
    *
    * @param  array $b1 -> One-Dimensional Array
    * @param  array $b2 as reference -> Two-Dimensional Array or Model which then gets converted to array
    * @param  string $b1val -> currently selected value of $b1 array before recursion
    *
    *
    ***/

    if(!is_array($b2)){
      $b2 = $b2->toArray();
    }


    if(is_array($b1)){
      foreach($b1 as $bkeyOneDimensional => $bvalOneDimensional){
        foreach($b2 as $bkeyTwoDimensional => &$bvalTwoDimensional){

          if(is_array($bvalTwoDimensional)){
             $this->recursiveUpdateArray($bkeyOneDimensional, $bvalTwoDimensional, $bvalOneDimensional);
          }elseif($bkeyOneDimensional === $bkeyTwoDimensional){
            $bvalTwoDimensional = $bvalOneDimensional;
          }
        }
      }
    }elseif(is_string($b1) && is_array($b2)){
      foreach($b2 as $b2key => &$b2val){
        if($b1 === $b2key){
          $b2val = $b1val;
        }
      }
    }

  }

And here is how you would have to call it inside your workflow:

recursiveUpdateArray($oneDimArray, $toBeUpdatedTwoDimArray);

Since the function takes in your $toBeUpdatedTwoDimArray as a reference, you don't need to return into anything, the twoDimArray will have received the updates automatically after the function has finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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