associative

How to convert a simple array to an associative array?

两盒软妹~` 提交于 2019-11-29 10:53:04
问题 What is the fastest way to convert a simple array to an associative array in PHP so that values can be checked in the isset($array[$value]) ? I.e. fastest way to do the following conversion: $array = array(1, 2, 3, 4, 5); $assoc = array(); foreach ($array as $i => $value) { $assoc[$value] = 1; } 回答1: array_flip() is exactly doing that: array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys. Note that the values of trans need to be

python sqlalchemy get column names dynamically?

本秂侑毒 提交于 2019-11-29 09:42:12
#!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import create_engine connection = create_engine('mysql://user:passwd@localhost:3306/db').connect() result = connection.execute("select * from table") for v in result: print v['id'] print v['name'] connection.close() how i can get TABLES COLUMNS NAMES dynamically? in this case id and name You can either find the columns by calling result.keys() or you can access them through calling v.keys() inside the for loop. Here's an example using items() : for v in result: for column, value in v.items(): print('{0}: {1}'.format(column, value))

Accessing associative arrays in PHP

只谈情不闲聊 提交于 2019-11-29 06:41:21
I want to access the index 'memo' in the associative array in PHP below $variables["thelistitems"]; print_r($variables["thelistitems"]); Output Array ( [0] => Array ( [productid] => prod:c6dbdd62-dc13-6421-5a94-c8cd871a59d3 [memo] => dummy [taxable] => 0 [unitweight] => 0 [unitcost] => 450.02 [unitprice] => 445.02 [quantity] => 1 ) ) What you essentially have is an array of associative arrays. So to access the first memo, it's $variables["thelistitems"][0]["memo"] To access each memo, you'd do something like this foreach($variables["thelistitems"] as $listitem) { $memo = $listitem["memo"]; }

php associative array key order (not sort)

人走茶凉 提交于 2019-11-29 05:50:24
My array: $data = array('two' => 2, 'one' => 1, 'three' => 3); Now, with when I iterate the array, the first value that will come up will probably be $data['two'] // = 2 @ index[0] right? What if I want to move the $data[1] to the position of $data[0] ? To rephrase: How do I make the array look like this (so that 'one' comes up at $data[0]) $data = array('one' => 1, 'two' => 2, 'three' => 3 Why do I need this? I use code igniter, the table->generate built-in function takes an assoc array and creates a table but offers no method of arranging the columns. This is why I would like to move the

JS associative object with duplicate names

血红的双手。 提交于 2019-11-28 11:48:15
ok, so I have an object like: var myobject = { "field_1": "lorem ipsum", "field_2": 1, "field_2": 2, "field_2": 6 }; as you see there are duplicate names in the object, but with different values. If i go through it like (using jQuery): $.each(myobject, function(key, value) { console.log(key); console.log(myobject[key]); console.log(myobject[value]); } key - returns the correct key myobject[key] - returns the name for that key myobject[value] - returns the last elements', with that name, value meaning for field_2 it will return 6, though it'll print it 3 times, as it repeats 3 times in the

How to make a right-associative infix operator?

扶醉桌前 提交于 2019-11-28 06:47:31
I have an associative operation >> . The problem is that its cost linearly depends on the size of its left operand. So an expression formed by a sequence of n applications of >> like a >> a >> a >> a >> a >> ... >> a it has quadratic cost in terms of n , because by default infix operators are left-associative . How to make it right-associative so that the cost of such an expression is kept linear in terms of n ? Petr Pudlák I found a solution. Scala reference says in section 6.12.3 Infix Operations : The associativity of an operator is determined by the operator’s last character. Operators

How to remove duplicate values from an associative array based on a specific value?

不问归期 提交于 2019-11-28 06:00:53
问题 I have an array that looks just like that: array(3) { ["fk_article_id"]=> string(1) "4" ["first_name"]=> string(6) "Ulrike" ["last_name"]=> string(10) "Grasberger" } array(3) { ["fk_article_id"]=> string(1) "9" ["first_name"]=> string(5) "Frank" ["last_name"]=> string(9) "Neubacher" } array(3) { ["fk_article_id"]=> string(3) "896" ["first_name"]=> string(2) "D." ["last_name"]=> string(5) "Bauer" } array(3) { ["fk_article_id"]=> string(3) "896" ["first_name"]=> string(2) "S." ["last_name"]=>

PHP - Return array of parents from multidimensional associative array for breadcrumb list

允我心安 提交于 2019-11-28 05:34:37
问题 I'm trying to create dynamic breadcrumbs from an array. So I can add to the array and not have to manually update the breadcrumbs. Okay, here's a snippet of my array: (It won't go much deeper) $menu = array( 'Dashboard' => array( 'Projects' => array( 'Project 1' => array( 'Project settings' => 'projects/project_1/settings', 'Issue Tracker' => 'projects/project_1/issue_tracker', 'Customize page' => 'projects/project_1', 'Manage files' => 'projects/project_1/files', ), 'Project 2' => array(

python sqlalchemy get column names dynamically?

旧时模样 提交于 2019-11-28 02:56:55
问题 #!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import create_engine connection = create_engine('mysql://user:passwd@localhost:3306/db').connect() result = connection.execute("select * from table") for v in result: print v['id'] print v['name'] connection.close() how i can get TABLES COLUMNS NAMES dynamically? in this case id and name 回答1: You can either find the columns by calling result.keys() or you can access them through calling v.keys() inside the for loop. Here's an

bash4 read file into associative array

帅比萌擦擦* 提交于 2019-11-27 15:11:10
问题 I am able to read file into a regular array with a single statement: local -a ary readarray -t ary < $fileName Not happening is reading a file into assoc. array. I have control over file creation and so would like to do as simply as possible w/o loops if possible at all. So file content can be following to be read in as: keyname=valueInfo But I am willing to replace = with another string if cuts down on code, especially in a single line code as above. And ... So would it be possible to read