associative-array

php - create assciative array while loop

被刻印的时光 ゝ 提交于 2019-12-24 00:06:33
问题 I am trying to create 2 new arrays out of one existing array ($array), using the following "foreach" loop. However I am not sure it is correct: $emails = array(); $numbers = array(); while($array){ $entry = $array['entry1']; $number = number($entry); if(isset($number) && (strlen($number) > 9)){ $numbers[] = array('entry1' => $entry, 'number' => $number); } else{ $email = email($entry); $emails[] = array('entry1' => $entry, 'email' => $email); } } should the internal arrays have [] ? do I even

Complexity of inserting sorted range into associative container

让人想犯罪 __ 提交于 2019-12-23 16:26:54
问题 The standard specifies (23.4.4.2:5, etc.) that constructing all four ordered associative containers ( map , multimap , set , multiset ) from a range [first, last) shall be linear in N = last - first if the range is already sorted. For merging a range (e.g. a container of the same type) into an existing container, however, 23.2.4:8 table 102 specifies only that insert ing a range [i, j) into the container shall have complexity N log(a.size() + N) where N = distance(i, j) . This would seem to

PHP array: integer index vs string index

萝らか妹 提交于 2019-12-23 13:44:10
问题 Is there any difference between integer index and string index of PHP arrays (except of course the fact that the latter is called associative array )? So for example, what are the differences between the following two arrays: $intIndex[5] = "Hello"; $intIndex[6] = "World"; $intIndex[7] = "!"; And $strIndex['5'] = "Hello"; $strIndex['6'] = "World"; $strIndex['7'] = "!"; In the first case, what happens to $intIndex[0] to $intIndex[4] ? 回答1: From the manual (emphasis mine): The key can either be

PHP array: integer index vs string index

夙愿已清 提交于 2019-12-23 13:44:09
问题 Is there any difference between integer index and string index of PHP arrays (except of course the fact that the latter is called associative array )? So for example, what are the differences between the following two arrays: $intIndex[5] = "Hello"; $intIndex[6] = "World"; $intIndex[7] = "!"; And $strIndex['5'] = "Hello"; $strIndex['6'] = "World"; $strIndex['7'] = "!"; In the first case, what happens to $intIndex[0] to $intIndex[4] ? 回答1: From the manual (emphasis mine): The key can either be

Can I use an stl map if I plan to use arbitrary class objects as the key?

泄露秘密 提交于 2019-12-23 09:49:33
问题 I'm new to STL. The thing stumping me about using a map to store arbitrary objects: std::map<MyClassObj, MyDataObject> MyMap; is how I find objects. How would MyMap.find (MyClassObjInstance) work for instance? Do I need to implement my own iterator and provide some standard functions which would include some equivalence function? Any examples would be appreciated. Is there another method to store an associated list of arbitrary objects using standard libraries? I'm already using stl to

Quickest way to replace associative array keys with numerical keys

瘦欲@ 提交于 2019-12-22 09:45:56
问题 I have an array: array('something' => 'like this', 'something' => 'like this', 'something' => 'like this'); I would like to replace it (quickly as possible, using a simple inline function) to be like this: array(0 => 'like this', 1 => 'like this', 2 => 'like this'); Possible using any built-in php-array functions? 回答1: check out array_values $new_array=array_values($array); print_r($new_array); 回答2: $arr = array( 'something'=>'something', 'something'=>'something' ); $new_arr = array(); $new

Does php conserve order in associative array? [duplicate]

这一生的挚爱 提交于 2019-12-22 04:00:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Are PHP Associative Arrays ordered? If I add items to associative array with different keys, does order of addition conserved? How can I access "previous" and "next" elements of given element? 回答1: Yes, php arrays have an implicit order. Use reset, next, prev and current - or just a foreach loop - to inspect it. 回答2: Yes, it does preserve the order. you can think of php arrays as ordered hash maps. You can think

Search of Dictionary Keys python

耗尽温柔 提交于 2019-12-21 09:22:25
问题 I want to know how I could perform some kind of index on keys from a python dictionary. The dictionary holds approx. 400,000 items, so I am trying to avoid a linear search. Basically, I am trying to find if the userinput is inside any of the dict keys. for keys in dict: if userinput in keys: DoSomething() break That would be an example of what I am trying to do. Is there a way to search in a more direct way, without a loop ? or what would be a more efficient way. Clarification: The userinput

Reference key from same array

你。 提交于 2019-12-21 05:21:52
问题 I'm trying to reference the key/value pair of an item in the same array: $glossary_args = array( 'name' => 'Glossary Terms', 'singular_name' => 'Glossary Term', 'add_new' => 'Add New Term', 'edit_item' => 'Edit Term', 'search_items' => 'Search'.$glossary_args["name"], ) Is this even possible? If so, how? 回答1: You can use the fact that assignment is itself an expression in PHP: $glossary_args = array( 'name' => ($name = 'Glossary Terms'), 'singular_name' => 'Glossary Term', 'add_new' => 'Add

Javascript: Adding to an associative array

浪子不回头ぞ 提交于 2019-12-21 03:46:26
问题 I have a function called insert which takes two parameters (name, telnumber). When I call this function I want to add to an associative array. So for example, when I do the following: insert("John", "999"); insert("Adam", "5433"); I want to it so be stored like this: [0] { name: John, number: 999 } [1] { name: Adam, number: 5433 } 回答1: Something like this should do the trick: var arr = []; function insert(name, number) { arr.push({ name: name, number: number }); } 回答2: Would use something