PHP Comparison of two dimensional arrays

I'm already puzzling over this.

I have two arrays:

Array 1 pulls from a CSV file.

  Array
 (
     [0] => Array
         (
             [uid] => cgajate
             [date] => 20120918
         )

     [1] => Array
         (
             [uid] => badrock5
             [date] => 20120920
         )

     [2] => Array
         (
             [uid] => ricoetc
             [date] => 20120921
         )

     [3] => Array
         (
             [uid] => ricoetc1
             [date] => 20120923
         )

     [4] => Array
         (
             [uid] => darbyfired
             [date] => 20120922
         )

     [5] => Array
         (
             [uid] => sagers.andrew
             [date] => 20120922
         )

     [6] => Array
         (
             [uid] => frankfurt9
             [date] => 20120923
         )

     [7] => Array
         (
             [uid] => beachboys
             [date] => 20120923
         )

     [8] => Array
         (
             [uid] => panterafan
             [date] => 20120923
         )

     [9] => Array
         (
             [uid] => kingsxrules
             [date] => 20120923
         )

     [10] => Array
         (
             [uid] => richard.bitto
             [date] => 20120924
         )

     [11] => Array
         (
             [uid] => christopher.boss
             [date] => 20120925
         )

     [12] => Array
         (
             [uid] => eric.robinson2
             [date] => 20120926
         )

 )

Array 2 retrieves from the SQL database.

  Array
 (
     [0] => Array
         (
             [uid] => cgajate
             [date] => 20120919
         )

     [1] => Array
         (
             [uid] => ricoetc
             [date] => 20120921
         )

     [2] => Array
         (
             [uid] => ricoetc1
             [date] => 20120922
         )

     [3] => Array
         (
             [uid] => frankfurt9
             [date] => 20120923
         )

     [4] => Array
         (
             [uid] => beachboys
             [date] => 20120923
         )

     [5] => Array
         (
             [uid] => panterafan
             [date] => 20120923
         )

     [6] => Array
         (
             [uid] => kingsxrules
             [date] => 20120923
         )

     [7] => Array
         (
             [uid] => eric.robinson2
             [date] => 20120926
         )

 )

What I, essentially, do is check if there are any UID matches. If there is a match, it will check which date is more relevant than the other (so it is essential which date is longer). Then it will add them to the array with data that the UIDs do not match with both arrays.

Sorry if this is hard to understand. This is a little hard to explain.

Any help is appreciated, thank you all in advance.

+4
source share
2 answers

Since PHP arrays themselves are hash maps, you can iterate through a single array and insert each date into a new array with the UID key:

$out = array(); foreach ($first_array as $x) { $out[$x['uid']] = $x['date']; } 

You can then iterate over the second array, checking to see if any of the UIDs exist as keys in the $out array. If the UID already exists, you can compare the dates and take any piece of data that you prefer. For example, something like:

 foreach ($second_array as $y) { if (array_key_exists($y['uid'], $out)) { if ($out[$y['uid']] < $y['date']) { $out[$y['uid']] = $y['date']; } } else { $out[$y['uid']] = $date; } } 

Then, to collapse the data back:

 $_out = array(); foreach ($out as $uid => $date) { $_out[] = array("uid" => $uid, "date" => $date); } $out = $_out; 
+3
source

It is a little dirty, but it works.

 <?php $arr1 = array( array("uid" => "cgajate", "date" => 20120918), array("uid" => "badrock5", "date" => 20120920), array("uid" => "ricoetc", "date" => 20120921), array("uid" => "ricoetc1", "date" => 20120923), array("uid" => "darbyfired", "date" => 20120922), array("uid" => "sagers.andrew", "date" => 20120922), array("uid" => "frankfurt9", "date" => 20120923), array("uid" => "beachboys", "date" => 20120923), array("uid" => "panterafan", "date" => 20120923), array("uid" => "kingsxrules", "date" => 20120923), array("uid" => "richard.bitto", "date" => 20120924), array("uid" => "christopher.boss", "date" => 20120925), array("uid" => "eric.robinson2", "date" => 20120926)); $arr2 = Array( array("uid" => "cgajate", "date" => 20120919), array("uid" => "ricoetc", "date" => 20120921), array("uid" => "ricoetc1", "date" => 20120922), array("uid" => "frankfurt9", "date" => 20120923), array("uid" => "beachboys", "date" => 20120923), array("uid" => "panterafan", "date" => 20120923), array("uid" => "kingsxrules", "date" => 20120923), array("uid" => "eric.robinson2", "date" => 20120926)); function flatten ($arr) { $new_arr = array (); foreach ($arr as $sub_arr) { $new_arr[$sub_arr["uid"]] = $sub_arr["date"]; } return $new_arr; } $flat_arr1 = flatten ($arr1); $flat_arr2 = flatten ($arr2); $arr3 = array (); foreach ($flat_arr1 as $key=>$value) { if (isset ($flat_arr2[$key])) { $value = $flat_arr1[$key] > $flat_arr2[$key] ? $flat_arr1[$key] : $flat_arr2[$key]; } $arr3[$key] = $value; } foreach ($flat_arr2 as $key=>$value) { if (isset ($flat_arr1[$key])) { $value = $flat_arr1[$key] > $flat_arr2[$key] ? $flat_arr1[$key] : $flat_arr2[$key]; } $arr3[$key] = $value; } ?> <pre><?php print_r($flat_arr1); ?></pre> <pre><?php print_r($flat_arr2); ?></pre> <pre><?php print_r($arr3); ?></pre> 
0
source

Source: https://habr.com/ru/post/1436914/


All Articles