Best way to initialize (empty) array in PHP

$myArray = []; Creates empty array. You can push values onto the array later, like so: $myArray[] = “tree”; $myArray[] = “house”; $myArray[] = “dog”; At this point, $myArray contains “tree”, “house” and “dog”. Each of the above commands appends to the array, preserving the items that were already there. Having come from other languages, … Read more

Better way to sum a property value in an array

I know that this question has an accepted answer but I thought I’d chip in with an alternative which uses array.reduce, seeing that summing an array is the canonical example for reduce: $scope.sum = function(items, prop){ return items.reduce( function(a, b){ return a + b[prop]; }, 0); }; $scope.travelerTotal = $scope.sum($scope.traveler, ‘Amount’); Fiddle

How can I sort arrays and data in PHP?

Basic one dimensional arrays $array = array(3, 5, 2, 8); Applicable sort functions: sort rsort asort arsort natsort natcasesort ksort krsort The difference between those is merely whether key-value associations are kept (the “a” functions), whether it sorts low-to-high or reverse (“r“), whether it sorts values or keys (“k“) and how it compares values (“nat” … Read more

How do I create an array of strings in C?

If you don’t want to change the strings, then you could simply do const char *a[2]; a[0] = “blah”; a[1] = “hmm”; When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings “blah” and “hmm”. … Read more

Conversion of System.Array to List

Save yourself some pain… using System.Linq; int[] ints = new [] { 10, 20, 10, 34, 113 }; List<int> lst = ints.OfType<int>().ToList(); // this isn’t going to be fast. Can also just… List<int> lst = new List<int> { 10, 20, 10, 34, 113 }; or… List<int> lst = new List<int>(); lst.Add(10); lst.Add(20); lst.Add(10); lst.Add(34); lst.Add(113); … Read more