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

Multidimensional associative arrays in Bash

You can’t do what you’re trying to do: bash arrays are one-dimensional $ declare -A PERSONS $ declare -A PERSON $ PERSON[“FNAME”]=’John’ $ PERSON[“LNAME”]=’Andrew’ $ declare -p PERSON declare -A PERSON='([FNAME]=”John” [LNAME]=”Andrew” )’ $ PERSONS[1]=([FNAME]=”John” [LNAME]=”Andrew” ) bash: PERSONS[1]: cannot assign list to array member You can fake multidimensionality by composing a suitable array index … Read more

Associative arrays in C

Glib’s hash table. implements a map interface or (associative array). And it’s most likely the most used hash table implementation for C. GHashTable *table=g_hash_table_new(g_str_hash, g_str_equal); /* put */ g_hash_table_insert(table,”SOME_KEY”,”SOME_VALUE”); /* get */ gchar *value = (gchar *) g_hash_table_lookup(table,”SOME_KEY”);

Automatically sorted by values map in Java

Keep 2 data structures: A dictionary of words -> count. Just use an ordinary HashMap<String, Long>. An “array” to keep track of order, such that list[count] holds a Set<String> of words with that count. I’m writing this as though it were an array as a notational convenience. In fact, you probably don’t know an upper … Read more

Filter rows of a 2d array by the rows in another 2d array

Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same. http://php.net/manual/en/function.array-diff-assoc.php The (string) value of any array is “Array”. Thus, your call to array_diff_assoc is effectively comparing these two things: Array … Read more

Merge two numerically-keyed associative arrays and preserve the original keys

Just use: $output = array_merge($array1, $array2); That should solve it. Because you use string keys if one key occurs more than one time (like ’44’ in your example) one key will overwrite preceding ones with the same name. Because in your case they both have the same value anyway it doesn’t matter and it will … Read more

PHP combine two associative arrays into one array

array_merge() is more efficient but there are a couple of options: $array1 = array(“id1” => “value1”); $array2 = array(“id2” => “value2”, “id3” => “value3”, “id4” => “value4”); $array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/); $array4 = $array1 + $array2; echo ‘<pre>’; var_dump($array3); var_dump($array4); echo ‘</pre>’; // Results: array(4) { [“id1”]=> string(6) “value1” [“id2”]=> string(6) “value2” [“id3”]=> … Read more

tech