associative-array

How to iterate over associative arrays in Bash

喜你入骨 提交于 2019-11-26 16:51:27
Based on an associative array in a Bash script, I need to iterate over it to get the key and value. #!/bin/bash declare -A array array[foo]=bar array[bar]=foo I actually don't understand how to get the key while using a for-in loop. The keys are accessed using an exclamation point: ${!array[@]} , the values are accessed using ${array[@]} . You can iterate over the key/value pairs like this: for i in "${!array[@]}" do echo "key : $i" echo "value: ${array[$i]}" done Note the use of quotes around the variable in the for statement (plus the use of @ instead of * ). This is necessary in case any

How to sort a date array in PHP

大兔子大兔子 提交于 2019-11-26 16:20:47
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 get the array in this state: Array [ ['2008-02-28']='some text', ['2008-03-06']='some text' ] Perhaps when its in

Multi-dimensional associative arrays in JavaScript

限于喜欢 提交于 2019-11-26 15:44:49
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 reading tons of tutorial, all I could get was this: arr=[]; arr[1]['apple'] = 2; but arr['fred']['apple'] =

How can I put the results of a MySQLi prepared statement into an associative array?

戏子无情 提交于 2019-11-26 14:06:15
问题 I have a sql query and a mysqli prepared statement: $sql = 'SELECT photographers.photographer_id, photographers.photographer_name FROM photographers'; $stmt = $conn->stmt_init(); if ($stmt->prepare($sql)) { $stmt->bind_result($photographer_id, $photographer_name); $OK = $stmt->execute(); $stmt->fetch(); } How can I store the results in an associative array so I can loop it later and get to all the data returned by the sql string? 回答1: Try the following: $meta = $statement->result_metadata();

Java associative-array

馋奶兔 提交于 2019-11-26 13:59:34
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'; Johan Sjöberg 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 example (since you can replace String with any object that meet your needs) would be to declare: List<Map<String,

PHP: Get n-th item of an associative array

ぐ巨炮叔叔 提交于 2019-11-26 13:55:22
问题 If you have an associative array: Array ( [uid] => Marvelous [status] => 1 [set_later] => Array ( [0] => 1 [1] => 0 ) [op] => Submit [submit] => Submit ) And you want to access the 2nd item, how would you do it? $arr[1] doesn't seem to be working: foreach ($form_state['values']['set_later'] as $fieldKey => $setLater) { if (! $setLater) { $valueForAll = $form_state['values'][$fieldKey]; $_SESSION[SET_NOW_KEY][array_search($valueForAll, $form_state['values'])] = $valueForAll; // this isn't

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

☆樱花仙子☆ 提交于 2019-11-26 12:16:27
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. Jeremy Ruten 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; if ($depth > $max_depth) { $max_depth = $depth; } } } return $max_depth; } ?> Edit: Tested it

PHP prepend associative array with literal keys?

十年热恋 提交于 2019-11-26 12:09:37
问题 Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I\'m hoping for something that will work with literal keys. As an example I\'d like to do the following: $array1 = array(\'fruit3\'=>\'apple\', \'fruit4\'=>\'orange\'); $array2 = array(\'fruit1\'=>\'cherry\', \'fruit2\'=>\'blueberry\'); // prepend magic $resulting_array = (\'fruit1\'=>\'cherry\', \'fruit2\'=>\'blueberry\', \'fruit3\'=>\'apple\', \'fruit4\'=>

Interpolation (double quoted string) of Associative Arrays in PHP

两盒软妹~` 提交于 2019-11-26 11:50:07
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[ key1 ]"; # Error, unexpected T_ENCAPSED_AND_WHITESPACE print "He said $ha[key1]"; # !! correct (How Comes?) Inerestingly

How to pass an associative array as argument to a function in Bash?

你。 提交于 2019-11-26 10:57:01
问题 How do you pass an associative array as an argument to a function? Is this possible in Bash? The code below is not working as expected: function iterateArray { local ADATA=\"${@}\" # associative array for key in \"${!ADATA[@]}\" do echo \"key - ${key}\" echo \"value: ${ADATA[$key]}\" done } Passing associative arrays to a function like normal arrays does not work: iterateArray \"$A_DATA\" or iterateArray \"$A_DATA[@]\" 回答1: I had exactly the same problem last week and thought about it for