associative-array

Are there dictionaries in php?

帅比萌擦擦* 提交于 2019-11-30 02:37:35
For example: $names = {[bob:27, billy:43, sam:76]}; and then be able to reference it like this: $names[bob] Jacob http://php.net/manual/en/language.types.array.php <?php $array = array( "foo" => "bar", "bar" => "foo", ); // as of PHP 5.4 $array = [ "foo" => "bar", "bar" => "foo", ]; ?> Standard arrays can be used that way. Brént Russęll No, there are no dictionaries in php. The closest thing you have is an array. However, an array is different than a dictionary in that arrays have both an index and a key. Dictionaries only have keys and no index. What do I mean by that? $array = array( "foo" =

How to write a good PHP database insert using an associative array

半城伤御伤魂 提交于 2019-11-30 02:16:45
In PHP, I want to insert into a database using data contained in a associative array of field/value pairs. Example: $_fields = array('field1'=>'value1','field2'=>'value2','field3'=>'value3'); The resulting SQL insert should look as follows: INSERT INTO table (field1,field2,field3) VALUES ('value1','value2','value3'); I have come up with the following PHP one-liner: mysql_query("INSERT INTO table (".implode(',',array_keys($_fields)).") VALUES (".implode(',',array_values($_fields)).")"); It separates the keys and values of the the associative array and implodes to generate a comma-separated

Get object keys with the highest value in Javascript

Deadly 提交于 2019-11-29 23:27:52
问题 Imagine an object like this: var values = { "2": 1, "53": 2, "56": 4, "57": 9, "61": 2, "62": 16, "63": 2, "398": 24, ... } My goal is, to find the 10 object keys, which have the highest value. In this case: 398, then 62 and so on (= [398, 62, ...] ). I know how I can put this into an array, don't know how to receive the property key though. Important: I can't change the format because it's a server response. I tried with a for (key in values) {} loop but have no idea how to move on. This

How to update specific key's value in an associative array in PHP?

≯℡__Kan透↙ 提交于 2019-11-29 20:52:24
I've a following associative array named $data Array ( [0] => Array ( [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506 [transaction_no] => 19500912050218 [transaction_total_amount] => 589.00 [transaction_date] => 1335932419 [transaction_status] => cancelled ) [1] => Array ( [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d [transaction_no] => 36010512050819 [transaction_total_amount] => 79.00 [transaction_date] => 1336476696 [transaction_status] => cancelled ) [2] => Array ( [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d [transaction_no] => 19020512050820

JSON to PHP Associative array

為{幸葍}努か 提交于 2019-11-29 15:48:40
问题 would any of you know a good way to put this into an associative array . I have tried json_decode but found it to not be much help. This is the data i need to put into an associative array: { "data": [ { "name": "Joe Bloggs", "id": "203403465" }, { "name": "Fred Bloggs", "id": "254706567" }, { "name": "Barny Rubble", "id": "453363843" }, { "name": "Homer Simpson", "id": "263508546" } ] } EDIT: After I accepted the answer, I remembered why I thought that the json_decode wasn't working. Instead

What's the difference between objects and associated array in javascript?

六眼飞鱼酱① 提交于 2019-11-29 13:35:55
The Confusing discussion In this question , there is a discussion on the concepts of associated array and object in javaScript which I got a bit confused. In this example code: var check = { pattern : { name: /^[a-zA-Z-\s]{1,20}$/, email: /^[a-zA-Z0-9._(-)]+@[a-zA-Z0-9.(-)]+\.[a-zA-Z]{1,4}$/, pass: /.{6,40}/, url: /^[(-)\w&:\/\.=\?,#+]{1,}$/, aml: /<(.+)_([a-z]){1}>$/ } }; Here is the discussion makes me confused: @steven.yang the outer object is not an associative array in your sample, but that is what is being asked for @sissonb what do you mean by 'outer object is not an associative array'?

Dynamically creating/inserting into an associative array in PHP

我的梦境 提交于 2019-11-29 11:21:31
I'm trying to build an associative array in PHP dynamically, and not quite getting my strategy right. Basically, I want to insert a value at a certain depth in the array structure, for instance: $array['first']['second']['third'] = $val; Now, the thing is, I'm not sure if that depth is available, and if it isn't, I want to create the keys (and arrays) for each level, and finally insert the value at the correct level. Since I'm doing this quite a lot in my code, I grew tired of doing a whole bunch of "array_key_exists", so I wanted to do a function that builds the array for me, given a list of

Bash indirect reference to an associative array

元气小坏坏 提交于 2019-11-29 07:55:37
In this very simplified example, I need to address both key and value of an array element: declare -A writer writer[H.P.]=Lovecraft writer[Stephen]=King writer[Clive]=Barker writer[Jack]=Ketchum for i in ${!writer[@]} do echo "$i ${writer[$i]}" done fullname() { pointer=$1[@] for i in "${!pointer}" do echo "? $i" done } fullname writer The function must display the output in the same format as the example loop before it, and it should receive either array name, list of keys or values, all of which I tried, without success. Any suggestions are greatly appreciated. indir_keys() { eval "echo \${!

MySQLI Prepared Statement: num_rows & fetch_assoc

会有一股神秘感。 提交于 2019-11-29 07:27:13
Below is some poorly written and heavily misunderstood PHP code with no error checking. To be honest, I'm struggling a little getting my head around the maze of PHP->MySQLi functions! Could someone please provide an example of how one would use prepared statements to collect results in an associative array whilst also getting a row count from $stmt? The code below is what I'm playing around with. I think the bit that's throwing me off is using $stmt values after store_result and then trying to collect an assoc array, and I'm not too sure why... $mysqli = mysqli_connect($config['host'], $config

Can you convert C# dictionary to Javascript associative array using asp.net mvc Json()

£可爱£侵袭症+ 提交于 2019-11-29 07:06:46
问题 I recently asked this question, but after some of the responses and some research, i wanted to change what i was actually asking. i have seen a number of blog posts about sending associative arrays from javascript to C# controller action but i want the opposite. I want to return json to a client as a dictionary (from my research the javascript equivalent of dictionary is an associative array). when i take a regular dictionary in c sharp and call Json() on it and try to return it to javascript