PHP code example of breier / extended-array

1. Go to this page and download the library: Download breier/extended-array 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/ */

    

breier / extended-array example snippets


  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print($cities->arsort());
  /**
   * {
   *   "3": "Tokyo",
   *   "1": "Paris",
   *   "0": "Dublin",
   *   "2": "Cairo"
   * }
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  var_dump($cities->contains('Cairo')); // true
  var_dump($cities->contains('Kyoto')); // false
  var_dump($cities->contains($cities->{1})); // true
  

  
  $array1 = new ExtendedArray(["a" => "green", "red", "blue", 7 => ["red", "yellow"]]);
  $array2 = new ExtendedArray(["b" => "green", "yellow", "red"]);
  print($array1->diff($array2));
  /**
   * {
   *   "1": "blue",
   *   "7": {"0":"red","1":"yellow"}
   * }
   */
  

  
  print(ExtendedArray::explode(',', 'Dublin,Paris,Cairo,Tokyo'));
  /**
   * {
   *   "0": "Dublin",
   *   "1": "Paris",
   *   "2": "Cairo",
   *   "3": "Tokyo"
   * }
   */
  

  
  print(ExtendedArray::fill(0, 3, "test"));
  /**
   * {
   *   "0": "test",
   *   "1": "test",
   *   "2": "test"
   * }
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print(
    $cities->filter(
      function ($item) {
        return strlen($item) == 5;
      }
    )
  );
  /**
   * {
   *   "1": "Paris",
   *   "2": "Cairo",
   *   "3": "Tokyo"
   * }
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  $countries = new ExtendedArray(['Ireland', 'France', 'Egypt', 'Japan']);
  $places = new ExtendedArray(['cities' => $cities, 'countries' => $countries]);
  print(
    $places->filterWithObjects(
      function ($item) {
        return $item->count();
      }
    )
  );
  /**
   * {
   *   "cities": {
   *     "0": "Dublin",
   *     "1": "Paris",
   *     "2": "Cairo",
   *     "3": "Tokyo"
   *   },
   *   "countries": {
   *     "0": "Ireland",
   *     "1": "France",
   *     "2": "Egypt",
   *     "3": "Japan"
   *   }
   * }
   */
  

  
  $jsonCities = '{"Dublin","Paris","Cairo","Tokyo"}';
  print(
    ExtendedArray::fromJSON($jsonCities)
  );
  /**
   * {
   *   "0": "Dublin",
   *   "1": "Paris",
   *   "2": "Cairo",
   *   "3": "Tokyo"
   * }
   */
  

  
  $plainArray = ['Dublin', 'Paris', 'Cairo', 'Tokyo'];
  $cities = new ExtendedArray($plainArray);
  var_dump(ExtendedArray::isArray($plainArray)); // true
  var_dump(ExtendedArray::isArray($cities));     // true
  var_dump(ExtendedArray::isArray('not array')); // false
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print($cities->implode(',')); // Dublin,Paris,Cairo,Tokyo
  

  
  $cities = new ExtendedArray(
    [
      'Ireland' => 'Dublin',
      'France' => 'Paris',
      'Egypt' => 'Cairo',
      'Japan' => 'Tokyo'
    ]
  );
  print($cities->keys());
  /**
   * {
   *   "0": "Ireland",
   *   "1": "France",
   *   "2": "Egypt",
   *   "3": "Japan"
   * }
   */
  

  
  $cities = new ExtendedArray(
    [
      'Ireland' => 'Dublin',
      'France' => 'Paris',
      'Egypt' => 'Cairo',
      'Japan' => 'Tokyo'
    ]
  );
  print($cities->krsort());
  /**
   * {
   *   "Japan": "Tokyo",
   *   "Ireland": "Dublin",
   *   "France": "Paris",
   *   "Egypt": "Cairo"
   * }
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print(
    $cities->map(
      function ($item) {
        return strrev($item);
      }
    )
  );
  /**
   * {
   *   "0": "nilbuD",
   *   "1": "siraP",
   *   "2": "oriaC",
   *   "3": "oykoT"
   * }
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  $countries = new ExtendedArray(['Ireland', 'France', 'Egypt', 'Japan']);
  $places = new ExtendedArray(['cities' => $cities, 'countries' => $countries]);
  print(
    $places->mapWithObjects(
      function ($item) {
        return $item->filter(
          function ($element) {
            return strlen($element) == 5;
          }
        )->jsonEncode();
      }
    )
  );
  /**
   * {
   *   "cities": "{\"1\":\"Paris\",\"2\":\"Cairo\",\"3\":\"Tokyo\"}",
   *   "countries": "{\"2\":\"Egypt\",\"3\":\"Japan\"}"
   * }
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print($cities->offsetGetFirst());
  /**
   * Dublin
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print($cities->offsetGetLast());
  /**
   * Tokyo
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print($cities->offsetGetPosition(1));
  /**
   * Paris
   */
  

  
  $cities = new ExtendedArray(['Dublin', 'Paris', 'Cairo', 'Tokyo']);
  print($cities->shuffle());
  /**
   * {
   *   "1": "Paris",
   *   "0": "Dublin",
   *   "3": "Tokyo",
   *   "2": "Cairo"
   * }
   */
  

  
  $cities = new ExtendedArray(
    [
      'Egypt' => 'Cairo',
      'Japan' => 'Tokyo',
      'France' => 'Paris',
      'Ireland' => 'Dublin'
    ]
  );
  print($cities->values());
  /**
   * {
   *   "0": "Cairo",
   *   "1": "Tokyo",
   *   "2": "Paris",
   *   "3": "Dublin"
   * }
   */