associative-array

PHP combine two associative arrays into one array

帅比萌擦擦* 提交于 2019-11-26 09:08:06
问题 $array1 = array(\"$name1\" => \"$id1\"); $array2 = array(\"$name2\" => \"$id2\", \"$name3\" => \"$id3\"); I need a new array combining all together, i.e. it would be $array3 = array(\"$name1\" => \"$id1\", \"$name2\" => \"$id2\", \"$name3\" => \"$id3\"); What is the best way to do this? Sorry, I forgot, the ids will never match each other, but technically the names could, yet would not be likely, and they all need to be listed in one array. I looked at array_merge but wasn\'t sure if that was

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

≡放荡痞女 提交于 2019-11-26 08:56:56
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. mario Using a simple regex via preg_match_all and array_combine is often the shortest and quickest option: preg_match_all("/([^\\\\]+)\\\\([^\\\\]+)/", $string, $p); $array = array_combine($p[1], $p[2]); Now this is of course a special case. Both keys and values are separated by a \

Convert an associative array to a simple array of its values in php

孤街浪徒 提交于 2019-11-26 08:19:49
问题 I would like to convert the array: Array ( [category] => category [post_tag] => post_tag [nav_menu] => nav_menu [link_category] => link_category [post_format] => post_format ) to array(category, post_tag, nav_menu, link_category, post_format) I tried $myarray = \'array(\'. implode(\', \',get_taxonomies(\'\',\'names\')) .\')\'; which echos out: array(category, post_tag, nav_menu, link_category, post_format) So I can do echo $myarray; echo \'array(category, post_tag, nav_menu, link_category,

Dynamically creating keys in JavaScript associative array

限于喜欢 提交于 2019-11-26 07:50:35
问题 How can I dynamically create keys in javascript associative arrays? All the documentation I\'ve found so far is to update keys that are already created: arr[\'key\'] = val; I have a string like this \" name = oscar \" And I want to end up with something like this: { name: \'whatever\' } That is I split the string and get the first element, and I want to put that in a dictionary. Code var text = \' name = oscar \' var dict = new Array(); var keyValuePair = text.split(\' = \'); dict[

Iterating over a complex Associative Array in PHP

人走茶凉 提交于 2019-11-26 07:34:42
问题 Is there an easy way to iterate over an associative array of this structure in PHP: The array $searches has a numbered index, with between 4 and 5 associative parts. So I not only need to iterate over $searches[0] through $searches[n] , but also $searches[0][\"part0\"] through $searches[n][\"partn\"] . The hard part is that different indexes have different numbers of parts (some might be missing one or two). Thoughts on doing this in a way that\'s nice, neat, and understandable? 回答1: Nest two

How to concatenate properties from multiple JavaScript objects

ⅰ亾dé卋堺 提交于 2019-11-26 07:23:04
问题 I am looking for the best way to \"add\" multiple JavaScript objects (associative arrays). For example, given: a = { \"one\" : 1, \"two\" : 2 }; b = { \"three\" : 3 }; c = { \"four\" : 4, \"five\" : 5 }; what is the best way to compute: { \"one\" : 1, \"two\" : 2, \"three\" : 3, \"four\" : 4, \"five\" : 5 } 回答1: ECMAscript 6 introduced Object.assign() to achieve this natively in Javascript. The Object.assign() method is used to copy the values of all enumerable own properties from one or more

Extract subset of key-value pairs from Python dictionary object?

核能气质少年 提交于 2019-11-26 06:54:07
问题 I have a big dictionary object that has several key value pairs (about 16), but I am only interested in 3 of them. What is the best way (shortest/efficient/most elegant) to achieve that? The best I know is: bigdict = {\'a\':1,\'b\':2,....,\'z\':26} subdict = {\'l\':bigdict[\'l\'], \'m\':bigdict[\'m\'], \'n\':bigdict[\'n\']} I am sure there is a more elegant way than this. Ideas? 回答1: You could try: dict((k, bigdict[k]) for k in ('l', 'm', 'n')) ... or in Python 3 Python versions 2.7 or later

Hash Table/Associative Array in VBA

北战南征 提交于 2019-11-26 06:37:35
问题 I can\'t seem to find the documentation explaining how to create a hash table or associative array in VBA. Is it even possible? Can you link to an article or better yet post the code? 回答1: I think you are looking for the Dictionary object, found in the Microsoft Scripting Runtime library. (Add a reference to your project from the Tools...References menu in the VBE.) It pretty much works with any simple value that can fit in a variant (Keys can't be arrays, and trying to make them objects

Associative arrays in Shell scripts

假如想象 提交于 2019-11-26 06:04:25
问题 We required a script that simulates Associative arrays or Map like data structure for Shell Scripting, any body? 回答1: To add to Irfan's answer, here is a shorter and faster version of get() since it requires no iteration over the map contents: get() { mapName=$1; key=$2 map=${!mapName} value="$(echo $map |sed -e "s/.*--${key}=\([^ ]*\).*/\1/" -e 's/:SP:/ /g' )" } 回答2: Another option, if portability is not your main concern, is to use associative arrays that are built in to the shell. This

How do I remove objects from a javascript associative array?

人走茶凉 提交于 2019-11-26 04:58:01
问题 Suppose I have this code: var myArray = new Object(); myArray[\"firstname\"] = \"Bob\"; myArray[\"lastname\"] = \"Smith\"; myArray[\"age\"] = 25; Now if I wanted to remove \"lastname\"?....is there some equivalent of myArray[\"lastname\"].remove() ? (I need the element gone because the number of elements is important and I want to keep things clean.) 回答1: Use the "delete" keyword in Javascript. delete myArray["lastname"]; EDIT: In some JavaScript engine, the delete keyword might hurt