PHP code example of ajant / simple-array-library

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

    

ajant / simple-array-library example snippets


SimpleArrayLibrary::addConfigRow(array('foo' => array('baz' => 1)), array('foo', 'bar'), 2); // array('foo' => array('baz' => 1, 'bar' => 2))
$config = array();
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 'bar'), new stdClass());
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 'baz'), 1);
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 3), array(1, 2));
// $config = array('foo' => array('bar' => stdClass(), 'baz' => 1, 3 => array(1, 2)))
$config = SimpleArrayLibrary::addConfigRow($config, array('foo', 3), false); // exception because of repeated  array('foo', 3) path

SimpleArrayLibrary::allElementsEqual(array(1, 1)); // true
SimpleArrayLibrary::allElementsEqual(array(1, 2)); // false
SimpleArrayLibrary::allElementsEqual(array(1, 1), 1); // true
SimpleArrayLibrary::allElementsEqual(array(1, 1), 2); // false

SimpleArrayLibrary::castColumns(array(array('a' => '2')), array('a' => SimpleArrayLibrary::TYPE_INT)); // array(array('a' => 2))
SimpleArrayLibrary::castColumns(array(array()), array('a' => SimpleArrayLibrary::TYPE_INT), false); // array(array())
SimpleArrayLibrary::castColumns(array(array('a' => '1'), array('b' => 'foo')), array('a' => SimpleArrayLibrary::TYPE_INT), false); // array(array('a' => 1), array('b' => 'foo'))

SimpleArrayLibrary::countMaxDepth(array()); // 1
SimpleArrayLibrary::countMaxDepth(array(1, array())); // 2
SimpleArrayLibrary::countMaxDepth(array(array('foo'))); // 2

SimpleArrayLibrary::countMaxDepthIterative(array()); // 1
SimpleArrayLibrary::countMaxDepthIterative(array(1, array())); // 2
SimpleArrayLibrary::countMaxDepthIterative(array(array('foo'))); // 2

SimpleArrayLibrary::countMinDepth(array()); // 1
SimpleArrayLibrary::countMinDepth(
    array(
        1,
        array()
    )
); // 1
SimpleArrayLibrary::countMinDepth(1); // 0
SimpleArrayLibrary::countMinDepth(array(), 1); // 2

SimpleArrayLibrary::deleteColumns(array(array('foo' => 2), array()), array('foo')); // array(array(), array())
SimpleArrayLibrary::deleteColumns(array(array(), array()), array('foo')); // array(array(), array())
SimpleArrayLibrary::deleteColumns(array(array('foo' => 2, 'bar' => 1), array()), array('foo', 'bar')); // array(array(), array())

SimpleArrayLibrary::getColumns(
    array(
        array('a' => 1),
        array(1)
    ),
    array('a')
); // array('a' => array(1))
SimpleArrayLibrary::getColumns(
    array(
        array(1, 2, 3, 4),
        array(1, 2, 3, 4),
        array(1, 2, 3, 4)
    ),
    array(0 ,2)
) // array(0 => array(1, 1, 1), 2 => array(3, 3, 3))
SimpleArrayLibrary::getColumns(
    array(
        array('a' => 1),
        array(1)
    ),
    array('a'),
    true
); // UnexpectedValueException is thrown, not all rows have all 

SimpleArrayLibrary::getRectangularDimensions(array(1)); // array(1)
SimpleArrayLibrary::getRectangularDimensions(array(1, array(1))); // -1, not "rectangular"
SimpleArrayLibrary::getRectangularDimensions(
    array(                    // 3 elements
        array(                // 1 element
            array(1, 2, 3, 4) // 4 elements
        ),
        array(                // 1 element
            array(1, 2, 3, 4) // 4 elements
        ),
        array(                // 1 element
            array(1, 2, 3, 4) // 4 elements
        )
    )
); // array(4, 1, 3)

SimpleArrayLibrary::hasAllKeys(array('a' => 1), array('a')); // true
SimpleArrayLibrary::hasAllKeys(array(), array()); // true
SimpleArrayLibrary::hasAllKeys(array('b' => 1), array('a', 'b')); // false

SimpleArrayLibrary::hasAllValues(array('a' => 1), array(1)); // true
SimpleArrayLibrary::hasAllValues(array(), array()); // true
SimpleArrayLibrary::hasAllValues(array('b', 1), array('a', 'b')); // false

SimpleArrayLibrary::hasAllValuesMultiDimensional(array('a' => array(1)), array(array(1))); // true
SimpleArrayLibrary::hasAllValuesMultiDimensional(array(array()), array(array())); // true
SimpleArrayLibrary::hasAllValuesMultiDimensional(array('b', 1), array('a', 'b')); // false

SimpleArrayLibrary::hasOnlyKeys(array('a' => 1, 2), array('a', 0)); // true
SimpleArrayLibrary::hasOnlyKeys(array('b' => 1, 1), array('b', 0, 1)); // false
SimpleArrayLibrary::hasOnlyKeys(array('b' => 1, 1), array('b')); // false

SimpleArrayLibrary::haveSameKeys(array('a' => 1), array('a' => 2)); // true
SimpleArrayLibrary::haveSameKeys(array(), array()); // true
SimpleArrayLibrary::haveSameKeys(array(1, 'a' => 1), array(2)); // false

SimpleArrayLibrary::haveSameValues(array('a' => 1), array(1)); // true
SimpleArrayLibrary::haveSameValues(array(), array()); // true
SimpleArrayLibrary::haveSameValues(array(1), array(2)); // false

SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('bar' => 2)); // array('foo' => 1, 'bar' => 2)
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), true, false); // array('foo' => 2)
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), false, true); // array('foo' => 1)
SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), false, false); // exception

SimpleArrayLibrary::isAssociative(array('a' => 1, array(1))); // true
SimpleArrayLibrary::isAssociative(array(1, 1)); // false
SimpleArrayLibrary::isAssociative(array()); // false

SimpleArrayLibrary::isNumeric(array('a' => 1, array(1))); // false
SimpleArrayLibrary::isNumeric(array(1, 1)); // true
SimpleArrayLibrary::isNumeric(array()); // false

SimpleArrayLibrary::isStructureSame(1, 'a'); // true
SimpleArrayLibrary::isStructureSame(array(1, array(1)), array(2, array(3)); // true
SimpleArrayLibrary::isStructureSame(array(1, array(1)), array(1, 1)); // false
SimpleArrayLibrary::isStructureSame(array(), array()); // true

SimpleArrayLibrary::isSubArray(array(2, 1), array(2)); // true
SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('c' => 1)); // false
SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('a' => 2)); // false

SimpleArrayLibrary::selectRandomArrayElements(array('foo' => 2), 1); // array('foo' => 2)
SimpleArrayLibrary::selectRandomArrayElements(array(1, 2), 1); // array(1) OR array(1 => 2)
SimpleArrayLibrary::selectRandomArrayElements(array('foo' => 2, 'bar'), 3); // array('foo' => 2, 'bar')

SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1); // array(array('foo' => 1), array('foo' => 1))
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, false, false); // array(array('foo' => 2), array())
SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, true, false); // array(array('foo' => 2), array('foo' => 1))

SimpleArrayLibrary::transpose(array(array(1, 2, 3). array(4, 5, 6))); // array(array(1, 4), array(2, 5), array(3, 6))