2

I have two arrays. I want to get the difference between them based on the sub-key name. I can use array_diff(array_column($array2, 'name'), array_column($array1, 'name')); but then my resulting array lacks the other key/values id and created because of the usage of array_column. I don't require the top level numeric index to be the same as the wanted array.

I have collections installed but I wasn't able to find a suitable method: https://laravel.com/docs/5.8/collections#available-methods

Array 1

array (
  2 => 
  array (
    'name' => 'sitebackup_2019_07_21_10_02_67.zip',
    'id' => '/restore_point_backups/sitebackup_2019_07_21_10_02_67.zip',
    'created' => 1563787267,
  ),
  1 => 
  array (
    'name' => 'sitebackup_2019_07_21_10_00_38.zip',
    'id' => '/restore_point_backups/sitebackup_2019_07_21_10_00_38.zip',
    'created' => 1563787197,
  ),
  0 => 
  array (
    'name' => 'sitebackup_2019_07_19_09_48_16.zip',
    'id' => '/restore_point_backups/sitebackup_2019_07_19_09_48_16.zip',
    'created' => 1563787125,
  ),
)

Array 2

array (
  0 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_19_08_52_37.zip',
    'created' => 1563519157,
  ),
  1 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_19_09_48_16.zip',
    'created' => 1563522496,
  ),
  2 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_21_10_00_38.zip',
    'created' => 1563696044,
  ),
  3 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_21_10_02_67.zip',
    'created' => 1563696184,
  ),
  4 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_21_10_12_29.zip',
    'created' => 1563696754,
  ),
  5 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_Jul_21_10_17_58.zip',
    'created' => 1563697083,
  ),
)

Wanted

array (
  0 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_19_08_52_37.zip',
    'created' => 1563519157,
  ),
  4 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_07_21_10_12_29.zip',
    'created' => 1563696754,
  ),
  5 => 
  array (
    'id' => 'C:\\xampp\\htdocs\\restore_point_backups_fagardesignscom\\files',
    'name' => 'sitebackup_2019_Jul_21_10_17_58.zip',
    'created' => 1563697083,
  ),
)
Alex
  • 9,215
  • 8
  • 39
  • 82

1 Answers1

3

Try the below code. It will work.

  //Get the column value from array 2 by name
    $array2_col = array_column($array_2, 'name');
    foreach($array as $key=>$value){
       //if array1 name value exist in array 2 get that key and delete from array 2 using unset.
          $index = array_search($value['name'],  $array2_col);
           if($index !== false ){
              unset($array_2[$index]);
           }
        }

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

DEMO

Output.

Array
(
    [0] => Array
        (
            [id] => C:\xampp\htdocs\restore_point_backups_fagardesignscom\files
            [name] => sitebackup_2019_07_19_08_52_37.zip
            [created] => 1563519157
        )

    [4] => Array
        (
            [id] => C:\xampp\htdocs\restore_point_backups_fagardesignscom\files
            [name] => sitebackup_2019_07_21_10_12_29.zip
            [created] => 1563696754
        )

    [5] => Array
        (
            [id] => C:\xampp\htdocs\restore_point_backups_fagardesignscom\files
            [name] => sitebackup_2019_Jul_21_10_17_58.zip
            [created] => 1563697083
        )

)

If you want to be an array index in sequence like 1,2,3, can use array_value.

print_r(array_values($array_2));

DEMO

Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11
  • @alex, when you get a chance try the above solution, it will work as you wanted. – Shivendra Singh Jul 23 '19 at 02:41
  • Glad to help you..:) – Shivendra Singh Jul 23 '19 at 03:27
  • a problem appeared in testing as one of the items has a 0 for index. as array search returns the index `0` then `if ($key)` erroneously evaluated to false. i replaced `array_search` with `if (in_array($value['name'], $localFilesCol)) {`. i think it will probably perform faster anyways. – Alex Jul 23 '19 at 04:23
  • yes i think it will be good, or we can also update the condition like. if($key !== false ){ unset($array_2[$key]); } – Shivendra Singh Jul 23 '19 at 04:36