Print the keys of an array

You can use PHP’s array_keys function to grab the keys, like so: foreach(array_keys($parameters) as $paramName) echo $paramName . “<br>”; Or, you can run through the array using a special foreach which allows you to separate the key and value for every element, like so: foreach($parameters as $paramName => $value) echo $paramName . “<br>”; Also, make … Read more

Loop a multidimensional array and only print two specific column values per row

Using a foreach loop without a key: foreach($array as $item) { echo $item[‘filename’]; echo $item[‘filepath’]; // To know what’s in $item echo ‘<pre>’; var_dump($item); } Using a foreach loop with a key: foreach($array as $i => $item) { echo $array[$i][‘filename’]; echo $array[$i][‘filepath’]; // $array[$i] is same as $item } Using a for loop: for ($i … Read more

Promise.all is returning an array of undefined and resolves before being done

Promise.all accepts an Array of Promise objects. You’re getting an Array of undefined because you’re not returning any Promise in your map callback: function addText(queries) { return Promise.all( { // Add `return` here or the `map` returns an Array of `undefined`. return models.queries .findById(query.queryId, { raw: true, attributes: [ “query” ] }) .then(function(queryFetched) { query.text … Read more

Custom key-sort a flat associative based on another array

Just use array_merge or array_replace. array_merge works by starting with the array you give it (in the proper order) and overwriting/adding the keys with data from your actual array: $customer[‘address’] = ‘123 fake st’; $customer[‘name’] = ‘Tim’; $customer[‘dob’] = ’12/08/1986′; $customer[‘dontSortMe’] = ‘this value doesnt need to be sorted’; $properOrderedArray = array_merge(array_flip(array(‘name’, ‘dob’, ‘address’)), $customer); … Read more

JavaScript: Remove duplicates of objects sharing same property value

This function removes duplicate values from an array by returning a new one. function removeDuplicatesBy(keyFn, array) { var mySet = new Set(); return array.filter(function(x) { var key = keyFn(x), isNew = !mySet.has(key); if (isNew) mySet.add(key); return isNew; }); } var values = [{color: “red”}, {color: “blue”}, {color: “red”, number: 2}]; var withoutDuplicates = removeDuplicatesBy(x => … Read more

How to delete an array in c#?

Say you call: void Foo(){ int[] a = new int[5]; } In C# there is no way to undefine the variable a. That means a will be defined in Foo even if you set a to null. However, at the end of Foo a will fall out of scope. That means no code can reference … Read more

How is numpy’s fancy indexing implemented?

You have three questions: 1. Which __xx__ method has numpy overridden/defined to handle fancy indexing? The indexing operator [] is overridable using __getitem__, __setitem__, and __delitem__. It can be fun to write a simple subclass that offers some introspection: >>> class VerboseList(list): … def __getitem__(self, key): … print(key) … return super().__getitem__(key) … Let’s make an … Read more