associative-array

How to sort a date array in PHP

二次信任 提交于 2019-11-26 04:46:28
问题 I have an array in this format: Array ( [0] => Array ( [28th February, 2009] => \'bla\' ) [1] => Array ( [19th March, 2009] => \'bla\' ) [2] => Array ( [5th April, 2009] => \'bla\' ) [3] => Array ( [19th April, 2009] => \'bla\' ) [4] => Array ( [2nd May, 2009] => \'bla\' ) ) I want to sort them out in the ascending order of the dates (based on the month, day, and year). What\'s the best way to do that? Originally the emails are being fetched in the MySQL date format, so its possible for me to

Multi-dimensional associative arrays in JavaScript

ぃ、小莉子 提交于 2019-11-26 04:36:19
问题 There is the following query results: (key1 and key2 could be any text) id key1 key2 value 1 fred apple 2 2 mary orange 10 3 fred banana 7 4 fred orange 4 5 sarah melon 5 ... and I wish to store the data in a grid (maybe as an array) looping all the records like this: apple orange banana melon fred 2 4 7 - mary - 10 - - sarah - - - 5 In PHP this would be really easy, using associative arrays: $result[\'fred\'][\'apple\'] = 2; But in JavaScript associative arrays like this doesn\'t work. After

Java associative-array

北慕城南 提交于 2019-11-26 03:47:09
问题 How can I create and fetch associative arrays in Java like I can in PHP? For example: $arr[0][\'name\'] = \'demo\'; $arr[0][\'fname\'] = \'fdemo\'; $arr[1][\'name\'] = \'test\'; $arr[1][\'fname\'] = \'fname\'; 回答1: Java doesn't support associative arrays, however this could easily be achieved using a Map . E.g., Map<String, String> map = new HashMap<String, String>(); map.put("name", "demo"); map.put("fname", "fdemo"); // etc map.get("name"); // returns "demo" Even more accurate to your

Interpolation (double quoted string) of Associative Arrays in PHP

有些话、适合烂在心里 提交于 2019-11-26 03:34:25
问题 When interpolating PHP\'s string-indexed array elements (5.3.3, Win32) the following behavior may be expected or not: $ha = array(\'key1\' => \'Hello to me\'); print $ha[\'key1\']; # correct (usual way) print $ha[key1]; # Warning, works (use of undefined constant) print \"He said {$ha[\'key1\']}\"; # correct (usual way) print \"He said {$ha[key1]}\"; # Warning, works (use of undefined constant) print \"He said $ha[\'key1\']\"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE print \"He said $ha[

Finding cartesian product with PHP associative arrays

穿精又带淫゛_ 提交于 2019-11-26 03:13:43
问题 Say that I have an array like the following: Array ( [arm] => Array ( [0] => A [1] => B [2] => C ) [gender] => Array ( [0] => Female [1] => Male ) [location] => Array ( [0] => Vancouver [1] => Calgary ) ) How can I find the cartesian product while preserving the keys of the outer associative array and using them in the inner ones? The result of the algorithm should be this: Array ( [0] => Array ( [arm] => A [gender] => Female [location] => Vancouver ) [1] => Array ( [arm] => A [gender] =>

Is there a way to find out how “deep” a PHP array is?

。_饼干妹妹 提交于 2019-11-26 02:55:14
问题 A PHP array can have arrays for its elements. And those arrays can have arrays and so on and so forth. Is there a way to find out the maximum nesting that exists in a PHP array? An example would be a function that returns 1 if the initial array does not have arrays as elements, 2 if at least one element is an array, and so on. 回答1: This should do it: <?php function array_depth(array $array) { $max_depth = 1; foreach ($array as $value) { if (is_array($value)) { $depth = array_depth($value) + 1

PHP Split Delimited String into Key/Value Pairs (Associative Array)

寵の児 提交于 2019-11-26 02:02:10
问题 I have a string like this: key1\\value1\\key2\\value2\\key3\\value3\\key4\\value4\\key5\\value5 And I\'d like it to be an associative array so that I can do: echo $myArray[\'key1\']; // prints value1 echo $myArray[\'key3\']; // prints value3 //etc... I know I can explode on the backslash, but not sure how to go from there. 回答1: Using a simple regex via preg_match_all and array_combine is often the shortest and quickest option: preg_match_all("/([^\\\\]+)\\\\([^\\\\]+)/", $string, $p); $array

How to define hash tables in Bash?

筅森魡賤 提交于 2019-11-26 01:29:16
问题 What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux). 回答1: Bash 4 Bash 4 natively supports this feature. Make sure your script's hashbang is #!/usr/bin/env bash or #!/bin/bash so you don't end up using sh . Make sure you're either executing your script directly, or execute script with bash script . (Not actually executing a Bash script with Bash does happen, and will be really confusing!) You declare an associative array by doing: declare -A animals

JavaScript associative array to JSON

荒凉一梦 提交于 2019-11-26 00:43:25
问题 How can I convert a JavaScript associative array into JSON? I have tried the following: var AssocArray = new Array(); AssocArray[\"a\"] = \"The letter A\" console.log(\"a = \" + AssocArray[\"a\"]); // result: \"a = The letter A\" JSON.stringify(AssocArray); // result: \"[]\" 回答1: Arrays should only have entries with numerical keys (arrays are also objects but you really should not mix these). If you convert an array to JSON, the process will only take numerical properties into account. Other

Rename a dictionary key

不问归期 提交于 2019-11-26 00:32:03
问题 Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value? In case of OrderedDict, do the same, while keeping that key\'s position. 回答1: For a regular dict, you can use: mydict[new_key] = mydict.pop(old_key) For an OrderedDict, I think you must build an entirely new one using a comprehension. >>> OrderedDict(zip('123', 'abc')) OrderedDict([('1', 'a'), ('2', 'b'), ('3', 'c')]) >>> oldkey,