C# how to get Byte[] from IntPtr

If it’s a byte[] array: byte[] managedArray = new byte[size]; Marshal.Copy(pnt, managedArray, 0, size); If it’s not byte[], the size parameter in of Marshal.Copy is the number of elements in the array, not the byte size. So, if you had an int[] array rather than a byte[] array, you would have to divide by 4 …

Read more

json_encode function not return Braces {} when array is empty in php

use the JSON_FORCE_OBJECT option of json_encode: json_encode($status, JSON_FORCE_OBJECT); Documentation JSON_FORCE_OBJECT (integer) Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0. Or, if you want to preserve your “other” arrays inside …

Read more

How to generate in PHP all combinations of items in multiple arrays

Here is recursive solution: function combinations($arrays, $i = 0) { if (!isset($arrays[$i])) { return array(); } if ($i == count($arrays) – 1) { return $arrays[$i]; } // get combinations from subsequent arrays $tmp = combinations($arrays, $i + 1); $result = array(); // concat each array from tmp with each element from $arrays[$i] foreach ($arrays[$i] as …

Read more

Remove all array elements except what I want?

By whitelisting the entries you do expect. <?php $post = array( ‘parent_id’ => 1, ‘type’ => ‘foo’, ‘title’ => ‘bar’, ‘body’ => ‘foo bar’, ‘tags’ => ‘foo, bar’, ‘one’ => ‘foo’, ‘two’ => ‘bar’, ‘three’ => ‘qux’ ); $whitelist = array( ‘parent_id’, ‘type’, ‘title’, ‘body’, ‘tags’ ); $filtered = array_intersect_key( $post, array_flip( $whitelist ) ); …

Read more

How do I add a tuple to a Swift Array?

Since this is still the top answer on google for adding tuples to an array, its worth noting that things have changed slightly in the latest release. namely: when declaring/instantiating arrays; the type is now nested within the braces: var stuff:[(name: String, value: Int)] = [] the compound assignment operator, +=, is now used for …

Read more