associative-array

PHP Count Number of True Values in a Boolean Array

我只是一个虾纸丫 提交于 2019-12-21 03:42:59
问题 I have an associative array in which I need to count the number of boolean true values within. The end result is to create an if statement in which would return true when only one true value exists within the array. It would need to return false if there are more then one true values within the array, or if there are no true values within the array. I know the best route would be to use count and in_array in some form. I'm not sure this would work, just off the top of my head but even if it

What is the easiest way to handle associative array in c#?

送分小仙女□ 提交于 2019-12-20 08:24:11
问题 I do not have a lot of experience with C#, yet I am used of working with associative arrays in PHP. I see that in C# the List class and the Array are available, but I would like to associate some string keys. What is the easiest way to handle this? Thx! 回答1: Use the Dictionary class. It should do what you need. Reference is here. So you can do something like this: IDictionary<string, int> dict = new Dictionary<string, int>(); dict["red"] = 10; dict["blue"] = 20; 回答2: A dictionary will work,

Redis how to store associative array? Set or Hash or List?

有些话、适合烂在心里 提交于 2019-12-20 08:10:59
问题 I'm a bit confused with all the available storing options of Redis. I want to do something simple and I don't want to over engineer it. I'm working with phpredis and Redis v2.8.6 . I have this simple associative array that I need to store. I also need to be able to retrieve an item by its key and loop over all the items. $a = array( '12345' => array( 'name' => 'Post A', 'val2' => 'blah blah', 'val3' => 'blah blah blah', ), '54321' => array( 'name' => 'Post B', 'val2' => 'blah blah', 'val3' =>

Does foreach() work for non-numerical array keys?

左心房为你撑大大i 提交于 2019-12-20 08:01:24
问题 I was wondering if foreach() works when the array looks like this: arr_name[eggs] = something arr_name[pencil] = something else Will foreach work if run as: foreach(arr_name as $key => $value) for they keys that have a non-numerical value ? 回答1: Yes, foreach supports any kind of key. In your case, $key will be a string, 'eggs' and 'pencil' respectively for each item. In fact, foreach was intended for use with arrays that have non-numerical keys which you can't easily iterate using for . 回答2:

iterating one key in a python multidimensional associative array

安稳与你 提交于 2019-12-20 07:26:03
问题 I'm dynamically creating a 2 dimensional associative array (dictionary?) I'm trying to loop through its keys - while keeping one of the indexes constant, so for instance all of the values associated to "key" with 'john' in its first bracket: myhash['john']['smith'] = "address 1" myhash['john']['doe'] = "address 2" how can i get all of the keys of the hash for each "key" keeping the first index as 'john' (i want all of the last names) Thanks 回答1: myhash['john'] is itself a dictionary. (You

bash associative array key string with colon is giving error

十年热恋 提交于 2019-12-20 04:59:10
问题 I am creating an associative array of source and destination MAC addresses. $ declare -a SrcDstMap $ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff" -bash: 9c: value too great for base (error token is "9c") $ SrcDstMap["fc:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff" -bash: fc:4e:20:73:e2:72: syntax error in expression (error token is ":4e:20:73:e2:72") How can I tell bash that the given key is a whole string. 回答1: That's not an associative array. You need to use declare -A , not declare -a . $

Does a PHP array need to be declared before use?

社会主义新天地 提交于 2019-12-19 15:23:34
问题 While writing a recent application I accidentally started filling an array before I had declared it. error_reporting ( E_ALL); $array['value'] = 'Test string'; I use E_ALL error reporting and an error was not thrown. Is this correct? And if so, are there any issues with declaring array values whilst never declaring the actual array? Perhaps it just doesn't follow good programming standards. 回答1: While writing a recent application I accidentally started filling an array before I had declared

Accessing Associative Arrays in GNU Parallel

折月煮酒 提交于 2019-12-19 10:29:07
问题 Assume the following in Bash: declare -A ar='([one]="1" [two]="2" )' declare -a ari='([0]="one" [1]="two")' for i in ${!ari[@]}; do echo $i ${ari[i]} ${ar[${ari[i]}]} done 0 one 1 1 two 2 Can the same be done with GNU Parallel, making sure to use the index of the associative array, not the sequence? Does the fact that arrays can't be exported make this difficult, if not impossible? 回答1: A lot has happened in 4 years. GNU Parallel 20190222 comes with env_parallel . This is a shell function

PHP Implode Associative Array

和自甴很熟 提交于 2019-12-19 05:17:10
问题 So I'm trying to create a function that generates a SQL query string based on a multi dimensional array. Example: function createQueryString($arrayToSelect, $table, $conditionalArray) { $queryStr = "SELECT ".implode(", ", $arrayToSelect)." FROM ".$table." WHERE "; $queryStr = $queryStr.implode(" AND ",$conditionalArray); /*NEED HELP HERE*/ return $queryStr; } $columnsToSelect = array('ID','username'); $table = 'table'; $conditions = array('lastname'=>'doe','zipcode'=>'12345'); echo

Check if associative array contains value, and retrieve key / position in array

霸气de小男生 提交于 2019-12-18 16:52:21
问题 I'm struggling to explain what I want to do here so apologies if I confuse you.. I'm just as confused myself I have an array like so: $foo = array( array('value' => 5680, 'text' => 'Red'), array('value' => 7899, 'text' => 'Green'), array('value' => 9968, 'text' => 'Blue'), array('value' => 4038, 'text' => 'Yellow'), ) I want to check if the array contains the value e.g. 7899 and also get the text linked to that value "Green" in the example above. 回答1: Try something like this $foo = array(