Search code examples
phplaravellaravel-5array-difference

How to diff default array in Laravel?


I've simple array:

$array = array(1,5,7,9,3,0);

And collection from database:

$collection = DB::table('numbers')->pluck('number');

Collection {#194 ▼
      #items: array:12 [▼
        0 => "1"
        1 => "2"
        2 => "3"
        3 => "4"
        4 => "5"
        5 => "6"
      ]
    }

How I can diff this arrays in Laravel ? And how I can diff default array($array) another array in laravel:

$another_array = DB::select("SELECT number FROM numbers");

array:12 [▼
      0 => {#194 ▼
        +"number": "0"
      }
      1 => {#207 ▼
        +"number": "-1"
      }
      2 => {#209 ▼
        +"number": "1"
      }
      3 => {#206 ▼
        +"number": "-2"
      }
    ]

Solution

  • Try:

    $collection = DB::table('numbers')->pluck('number')->toArray(); 
    $diff = array_diff($collection, $array);