PHP code example of yii2mod / yii2-helpers

1. Go to this page and download the library: Download yii2mod/yii2-helpers 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/ */

    

yii2mod / yii2-helpers example snippets


$array = ArrayHelper::add(['card' => 'Visa'], 'price', 200);
// ['card' => 'Visa', 'price' => 200]

ArrayHelper::average([1, 2, 3, 4, 5]);
// 3

$array = [
   ['score' => 10],
   ['score' => 30],
   ['score' => 50],
];

ArrayHelper::average($array, 'score');

// 30

$array = ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]);

// [1, 2, 3, 4, 5, 6]

$array = ['name' => 'Desk', 'price' => 100];

$array = ArrayHelper::except($array, ['price']);

// ['name' => 'Desk']

$array = ['products' => ['desk' => ['price' => 100]]];

$hasDesk = ArrayHelper::has($array, 'products.desk');

// true

$array = [100, 200, 300];
$value = ArrayHelper::first($array); // 100

// or apply custom callback

$value = ArrayHelper::first($array, function($key, $value) {
      return $value >= 150; // 200
});

$array = ['name' => 'Bob', 'languages' => ['PHP', 'Python']];

$array = ArrayHelper::flatten($array);

// ['Bob', 'PHP', 'Python'];

$array = [100, 200, 300];
$value = ArrayHelper::last($array); // 300

// or apply custom callback

$value = ArrayHelper::last($array, function($key, $value) {
      return $value; // 300
});

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$array = ArrayHelper::only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]

$array = ['one', 'two', 'three', 'four'];

$array = ArrayHelper::prepend($array, 'zero');

// $array: ['zero', 'one', 'two', 'three', 'four']

$array = [
   ['product_id' => 'prod-100', 'name' => 'Desk'],
   ['product_id' => 'prod-200', 'name' => 'Chair'],
];


$plucked = ArrayHelper::pluck($array, 'name');

// ['Desk', 'Chair']

$plucked = ArrayHelper::pluck($array, 'name', 'product_id');

// ['prod-100' => 'Desk', 'prod-200' => 'Chair']

$array = ['name' => 'Desk', 'price' => 100];

$name = ArrayHelper::pull($array, 'name');

// $name: Desk

// $array: ['price' => 100]

$array = ['products' => ['desk' => ['price' => 100]]];

ArrayHelper::set($array, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

$array = [
    ['name' => 'Desk'],
    ['name' => 'Chair'],
];

$array = ArrayHelper::sort($array, function ($value) {
    return $value['name'];
});

/*
    [
        ['name' => 'Chair'],
        ['name' => 'Desk'],
    ]
*/

$array = [
      [
          'Desc',
          'Chair',
      ],
      [
          'PHP',
          'Ruby',
          'JavaScript',
      ],
];

$array = ArrayHelper::sortRecursive($array);
        
/*
  [
      [
         'Chair',
         'Desc',
      ],
      [
         'JavaScript',
         'PHP',
         'Ruby',
      ]
  ];
*/

$array = ["100", 200, "300"];
$value = ArrayHelper::where($array, function($key, $value) {
   return is_string($value);
});
// Will be return Array ( [0] => 100 [2] => 300 );

$xml = '<?xml version="1.0"

php composer.phar