associative-array

Reference an associative array from inside a string?

限于喜欢 提交于 2019-12-10 11:22:34
问题 "CREATE TABLE IF NOT EXISTS $tables[users]"; Works but.. "CREATE TABLE IF NOT EXISTS $tables['users']"; Does not. I do not want to do this $usersTable = $tables['users']; "CREATE TABLE IF NOT EXISTS $usersTable"; I heard that it was considered bad practice to reference a key from an associative array without some sort of quotes around it. Is this true or is my first way of doing it preferred? 回答1: You can do this with braces: "CREATE TABLE IF NOT EXISTS {$tables['users']}"; Or through

Build associative array based on values of another associative array

﹥>﹥吖頭↗ 提交于 2019-12-10 09:15:58
问题 I'm looking for an elegant way to turn this array: Array ( [foo] => 1 [bar] => 1 [zim] => 3 [dib] => 6 [gir] => 1 [gaz] => 3 ) Into this array: Array ( [1] => Array ( foo, bar, gir ), [3] => Array ( zim, gaz ), [6] => Array ( dib ) ) Note: , there is no relationship between the keys or values. They are completely arbitrary and used as examples only. The resulting array should be an associative array grouped by the values of the input array. Thanks! 回答1: $input = array( 'foo' => 1, 'bar' => 1,

php simpleXMLElement to array: null value

回眸只為那壹抹淺笑 提交于 2019-12-09 09:55:00
问题 I've got following XML: <account> <id>123</id> <email></email> <status>ACTIVE</status> </account> I want to have it as an array variable. Therefore I read it with $xml = simplexml_load_file() . The simplest way to convert simpleXMLElement to an associative array I know is to grind it with: json_decode(json_encode((array) $xml),1); The problem is that I don't want to get the email key as an empty array, but rather as a NULL value. As SimpleXMLElement, it looks like: public 'email' => object

Use pop() with JavaScript Associative Arrays

China☆狼群 提交于 2019-12-09 08:38:22
问题 How can I do something like the following in JS? I would like to imitate .pop() on an object rather than an array. var deck = { 'cardK' :'13', 'cardQ' :'12', 'cardAJ':'11' }; var val = deck.pop(); console.log("Key" + val.key ); console.log("Value" + val.val ); It seems like it's not possible. 回答1: .pop is only available on an array. In JavaScript, objects (which are essentially associative arrays) are not ordered like an array, so there is no .pop method. You could use an array: var deck = [

Using an associative array as data for D3

喜你入骨 提交于 2019-12-09 07:51:32
问题 I have a very simple D3 example that first reads data into an associative array, then displays it in a bar graph. I can't seem to get anything to display using this method though. Instead, I have to insert a task in between: Read the data into an associative array, copy that data into a simple array, then display the bar graph using the simple array. chart.selectAll("div") .data(genreAssociative) .enter().append("div") .style("width", function(d) { return d * 10 + "px"; }) .text(function(d) {

Search and replace inside an associative array

北战南征 提交于 2019-12-09 03:51:07
问题 I need to search and replace inside an associative array. ex: $user = "user1"; // I've updated this $myarray = array("user1" => "search1", "user2" => "search2", "user3" => "search1" ) ; I want to replace search1 for search4 . How can I achieve this? UPDATE: I forgot to mention that the array has several search1 values and I just want to change the value where the key is == $user . Sorry for not mention this earlier. 回答1: $myarray = array("user1" => "search1", "user2" => "search2" ); foreach(

Change $key of associative array in a foreach loop in php

半城伤御伤魂 提交于 2019-12-09 02:47:58
问题 I have an array like this: array( 'firstName' => 'Joe', 'lastName' => 'Smith' ) I need to loop over every element in my array and in the end, the array should look like this: array( 'FirstName' => 'Joe', 'LastName' => 'Smith' ) Failed idea was: foreach($array as $key => $value) { $key = ucfirst($key); } This obviously will not work, because the array is not passed by reference. However, all these attempts also fail: foreach(&$array as $key => $value) { $key = ucfirst($key); } foreach($array

How to insert a new key value pair in array in php?

回眸只為那壹抹淺笑 提交于 2019-12-09 02:27:59
问题 I've an array as follows named $test_package_data . For the reference I'm printing first two elements of it: Array ( [0] => Array ( [test_pack_id] => 9f27643023a83addd5eed41c4aade840 [test_pack_name] => Exams Combo [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude. Total Tests in this Package : 26 [test_pack_type_id] => 3 [test_pack_image] => [test_pack_validity_year] => 0 [test_pack_validity_month] => 3 [test_pack_validity_days] => 0 [test

Why is this check for null associative array in PL/SQL failing?

坚强是说给别人听的谎言 提交于 2019-12-08 15:04:52
问题 I have an associative array created by a type of rowtype of a table column. To give an example, this is how it is(the table names are different, but the structure is the same): This is the DDL of the table CREATE TABLE employees ( id NUMBER, name VARCHAR2(240), salary NUMBER ); Here's what my procedure is doing: DECLARE TYPE table_of_emp IS TABLE OF employees%ROWTYPE INDEX BY BINARY_INTEGER; emp TABLE_OF_EMP; BEGIN IF emp IS NULL THEN dbms_output.Put_line('Null associative array'); ELSE dbms

c# associative array with dictionary

无人久伴 提交于 2019-12-08 14:23:45
问题 I want to build an Dictonary like this : Dictionary<String, ArrayList> myDic = new Dictionary<String, ArrayList>(); in the end i want a structure like : ["blabla"] => array(1,2,3) ["foo"] => array(1,4,6,8) ....... to build this i run in a loop and in every loop build some strings , first question : how to check every time if this string exists in the dictionary , if its not exists open a new entry in dictionary with one element in the array list, if exists only add another element to the