working on it ...

Filters

Explore Public Snippets

Sort by

Found 15k snippets matching: array

    public by Zenitram.PRO  404445  3  5  1

    Adding an Element in an Array using a Function.

    Arrays of Objects Remember that an object is just another type, like a string or number but more complex. This means that just as we can make arrays of numbers and strings, we can also make arrays of objects. Here we have our Person constructor which should look familiar. We can use this constructor to make an array of Person objects, similar
    // Our person constructor
    function Person (name, age) {
        this.name = name;
        this.age = age;
    }
    
    // Now we can make an array of people
    var family = new Array();
    family[0] = new Person("alice", 40);
    family[1] = new Person("bob", 42);
    family[2] = new Person("michelle", 8);
    // add the last family member, "timmy", who is 6 years old
    family[3] = new Person("timmy", 6);
                                                    

    public by Tech_JA  345219  1  6  4

    Check if array is empty

    this snippet checks if the $arrayToTest is empty
    <?php
      $ArrayToTest = array('');
      $TestingArray = array_filter($ArrayToTest);
      if (!empty($TestingArray)) {
        echo "not empty";
      } else {
        echo "empty";
      }
    ?>            

    public by miquelbotanch  264224  5  3  0

    Parse git log with PHP to an array

    Parse git log with PHP to an array: git_history.php
    <?php
    // Orginal Author: Ngo Minh Nam
    $dir = "/path/to/your/repo/";
    chdir($dir);
    $output = array();
    exec('git log -200 --pretty=format:%at%n%an%n%ae%n%h%n%s',$output);
    $history = array();
    $field = 0;
    foreach ($output as $line) {
    	if ($field == 0) {	$commit["date"] = $line;		}else  // unixtimestamp format
    	if ($field == 1) {	$commit["author"] = $line;		}else 
    	if ($field == 2) {	$commit["email"] = $line;		}else 
    	if ($field == 3) {	$commit["version"] = $line;		}else 
    	if ($field == 4) {	$commit["subject"] = $line;
    						$history[] = $commit;
    						unset($commit);
    						$field=-1;
    	}
    	$field++;
    }
    print "<pre>".print_r($history,true)."</pre>";
    
    

    public by sTiLL-iLL  235390  1  7  15

    ES5 Array funKtion polly-fillerz...

    just in case your browser sucks
    
    // filler for Array.prototype.bind()
    
    if (!Function.prototype.bind) {
      Function.prototype.bind = function(that) {
        if (typeof this !== 'function') {
          throw new TypeError('obj is not callable');
        }
        var a_Argz = Array.prototype.slice.call(arguments, 1), noop = function() {},
            bFnky = this, bnd  = function() {
              return bFnky.apply(this instanceof noop && that? this: that,
                     a_Argz.concat(Array.prototype.slice.call(arguments)));
            };
    
        noop.prototype = this.prototype;
        bnd.prototype = new noop();
        return bnd;
      };
    }
    
    // different approach for isArray()...
    
    function isArray (list) {
      
    }
    
    if (!Array.isArray) {
      Array.isArray = function(argz) {
        return Object.prototype.toString.call(argz) === '[object Array]';
      };
    }
    
    function isArray (list) {
      if (Array.isArray) {
        return Array.isArray(list);
      }
    }
    
    function isArray (list) {
      if (Array.isArray) {
        return Array.isArray(list);
      }
      return Object.prototype.toString.call(list) === '[object Array]';
    }
    
    
    var isArray;
    var isArray = (function () {
      
    })();
    
    var isArray = (function () {
      if (Array.isArray) {
        return Array.isArray;
      }
    })();
    
    var isArray = (function () {
      if (Array.isArray) {
        return Array.isArray;
      }
      return function (list) {
        return Object.prototype.toString.call(list) === '[object Array]';
      };
    })();
    
    function (list) {
      return Object.prototype.toString.call(list) === '[object Array]';
    }                        

    public by sTiLL-iLL @ SniPitz-KND  181413  1  7  10

    "flatten" your complex arrays

    a flattening Array helper function
    // go flat yerself!
    
    function flatten(arrayToFlatten, rslt) {
        rslt = rslt || [];
        arrayToFlatten.forEach(function(itm) {
            if (Array.isArray(itm)){
                rslt = flatten(itm, rslt);
            }
            else {
                rslt.push(itm);
            }
        });
        return rslt;
    }
    
    // use it...
    
    var someArrayData = [[60, 121, 4, 832],[71, 188, 6, 33]];
    
    // [60, 121, 4, 832, 71, 188, 6, 33]
    
    console.log(flatten(someArrayData));            

    public by zonaro  197150  0  8  0

    Sum (Excel port for VB.NET)

    Like in Excel, this method sum the numbers of a ParamArray
    Function Sum(ByVal ParamArray Nums As Decimal()) As Decimal
            Dim s = 0
            For Each i As Decimal In Nums
                s += i
            Next
            Return s
        End Function            

    public by Geometry  110591  0  5  0

    IsMatrix: Determines whether an array is an multidimensional array.

    Determines whether an array is an multidimensional array.
    /// <summary>
    ///   Determines whether an array is an multidimensional array.
    /// </summary>
    /// 
    public static bool IsMatrix(this Array array)
    {
        return array.Rank > 1;
    }

    public by lbottaro  4524  5  7  5

    How to sort a Javascript array parsing elements using regex pattern

    The Javascript Array.sort() method accepts a callback function to compare two elements of the array using a specific logic. The callback function below may help you in sorting an array matching a particular regular expression pattern. The format of elements in array is x/y/z, where x, y and z are integers, eg "5/10/2". I need to sort the eleme
    var myCompare = function(a,b) {
    	var matchPattern_A = /^([0-9]+)\/([0-9]+)\/([0-9]+)$/g.exec(a);
    	var matchPattern_B = /^([0-9]+)\/([0-9]+)\/([0-9]+)$/g.exec(b);
    
    	if(matchPattern_A && matchPattern_B)
    	{
    		//Apply regex sort
    		//I need to sort based on match[1], match[2], match[3]
    		console.log('Apply regex sort');
    
    		if (parseInt(matchPattern_A[1], 10) < parseInt(matchPattern_B[1], 10))
    			return -1;
    		if (parseInt(matchPattern_A[1], 10) > parseInt(matchPattern_B[1], 10))
    			return 1;
    		else {
    			if (parseInt(matchPattern_A[2], 10) < parseInt(matchPattern_B[2], 10))
    				return -1;
    			if (parseInt(matchPattern_A[2], 10) > parseInt(matchPattern_B[2], 10))
    				return 1;
    			else {
    				if (parseInt(matchPattern_A[3], 10) < parseInt(matchPattern_B[3], 10))
    					return -1;
    				if (parseInt(matchPattern_A[3], 10) > parseInt(matchPattern_B[3], 10))
    					return 1;
    				return 0;
    			}
    		}
    	}
    	else {
    		//Apply basic lexicographic sort
    		console.log('Apply basic lexicographic sort');
    		if (a < b)
    			return -1;
    		if (a > b)
    			return 1;
    		return 0;
    	}
    };
    
    var myArr = [ '5/4/30', '5/10/2', '5/4/3', '1/3/8'];
    console.log(myArr);
    myArr.sort(this.myCompare);
    console.log(myArr);
    
    /*  Example,
        having an array as
        ['5/4/30', '5/10/2', '5/4/3', '1/3/8']
        it will be sorted as
        ['1/3/8', '5/4/3', '5/4/30', '5/10/2']
    */            

    public by Geometry  148406  0  3  0

    GetCommandBuffer: Gets the bytes representing the specified command. returned buffer can be used to streamline multiple writes into one Write on the Socket using the <see cref="M:Enyim.Caching.Memcached.PooledSocket.Write(IList&lt;ArraySegment&lt;b...

    Gets the bytes representing the specified command. returned buffer can be used to streamline multiple writes into one Write on the Socket using the The command to be converted. The buffer containing the bytes representing the command. The command must be terminated by \r\n. The Nagle algorithm is disabled on the socket to speed things up, so it's
    C#
    using System;
    using System.IO;
    using System.Text;
    using System.Collections.Generic;
    
    /// <summary>
    /// Gets the bytes representing the specified command. returned buffer can be used to streamline multiple writes into one Write on the Socket
    /// using the <see cref="M:Enyim.Caching.Memcached.PooledSocket.Write(IList&lt;ArraySegment&lt;byte&gt;&gt;)"/>
    /// </summary>
    /// <param name="value">The command to be converted.</param>
    /// <returns>The buffer containing the bytes representing the command. The command must be terminated by \r\n.</returns>
    /// <remarks>The Nagle algorithm is disabled on the socket to speed things up, so it's recommended to convert a command into a buffer
    /// and use the <see cref="M:Enyim.Caching.Memcached.PooledSocket.Write(IList&lt;ArraySegment&lt;byte&gt;&gt;)"/> to send the command and the additional buffers in one transaction.</remarks>
    public unsafe static IList<ArraySegment<byte>> GetCommandBuffer(string value)
    {
    	var data = new ArraySegment<byte>(Encoding.ASCII.GetBytes(value));
    
    	return new ArraySegment<byte>[] { data };
    }

    public by Geometry  76104  0  5  0

    Convert: Converts a array of signed 32-bit integer samples into a array of 32-bit floating point samples.

    Converts a array of signed 32-bit integer samples into a array of 32-bit floating point samples. The original sample. The resulting sample.
    /// <summary>
    ///   Converts a array of signed 32-bit integer samples
    ///   into a array of 32-bit floating point samples.
    /// </summary>
    /// 
    /// <param name="from">The original sample.</param>
    /// <param name="to">The resulting sample.</param>
    /// 
    public static void Convert(Int32[] from, Int32[] to)
    {
        for (int i = 0; i < from.Length; i++)
            to[i] = (int)((double)from[i] * 0x7FFFFFFF);
    }
    • Public Snippets
    • Channels Snippets