PHP code example of world-warm-worm / array-state-observer

1. Go to this page and download the library: Download world-warm-worm/array-state-observer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

world-warm-worm / array-state-observer example snippets




orldWarmWorm\ArrayStateObserver\SimpleArrayObserver;

// we have some dummy initial data. let it be three ids of something
$before = [1, 2, 3];

// let's change initial data through some job with it
$after = array_slice($before, 0, 2);

// here we have object of SimpleArrayObserver that contains calculated data
$observer = SimpleArrayObserver::init($before, $after);

// here we want to get all of calculated data
$result = $observer->all();

// that's it!
var_dump($result);

// short notation
// var_dump(SimpleArrayObserver::init($before, $after)->all());

/** 
  array(2) {
    ["added"]=>
    array(0) {
    }
    ["deleted"]=>
    array(1) {
      [2]=>
      string(1) "3"
    }
  }
 */

var_dump(SimpleArrayObserver::init($before, $after)->deleted());

/** 
  array(1) {
     [2]=>
     int(3)
   }
*/