associative-array

Change an associative array into an indexed array / get an Zend_Table_Row_Abstract as non-associative

大憨熊 提交于 2019-12-02 23:15:48
Hi out there in Stackland. I was wondering if there was either a function or an easy way to change an associative array into an indexed array. To elaborate, I'm using the Zend framework, and I've got a point in my site where I take out a row of an SQL table as an associative array. I've passed it to javascript via an echoed in JSON. However, I've noticed that I can see the names of my database's columns in Firebug. Having outsiders know the names of your tables and columns is a big security no-no, so I'd like to change it from SQLarray[user_id] SQLarray[block_id] SQLarray[b_price] etc. to

PHP CSV to Associative Array with Row Headings

怎甘沉沦 提交于 2019-12-02 22:44:42
问题 I have a CSV file with headings in both the columns and rows that i need as an associative array and the examples i have tried on SO haven't worked for me. The closest example i found was CSV to Associative Array but i couldn't print the data, so i must be missing something. Any help would be appreciated $all_rows = array(); $header = 1;//null; // has header if (($handle = fopen($csv_file, "r")) !== FALSE) { while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($header === null) {

Awk array iteration for multi-dimensional arrays

*爱你&永不变心* 提交于 2019-12-02 21:35:43
Awk offers associative indexing for array processing. Elements of 1 dimensional array can be iterated: e.g. for(index in arr1) print "arr1[" index "]=" arr1[index] But how this kind done for a two dimensional array? Does kind of syntax,given below work? for(index1 in arr2) for(index2 in arr2) arr2[index1,index2] AWK fakes multidimensional arrays by concatenating the indices with the character held in the SUBSEP variable (0x1c). You can iterate through a two-dimensional array using split like this (based on an example in the info gawk file): awk 'BEGIN { OFS=","; array[1,2]=3; array[2,3]=5;

traversing an array in php [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-02 18:46:32
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Loop an array of array So I know how to traverse an array of even key => value (associative), but I have a weird array where I need to walk through it and print out values: $object_array = array( 'type' => 'I am type', array( 'property' => 'value', 'property_2' => 'value_2' ) ); What I thought I could do is: foreach($object as $key=>$vlaue){ //what now? } So as you can see I am lost, how do I walk through the

Hash/associative array using several objects as key

天大地大妈咪最大 提交于 2019-12-02 13:55:16
问题 Is there a way of making an associative array where each key is a hash of several objects? I'm not interested in inspecting each object's state, but rather the object's identity. var myarray = {}; var a = new A(); var b = new B(); var c = new C(); // + is not right, but illustrates the hashing I'm after. myarray[a + b + c] = 42; The + operator is not right. In java I would arithmetically combine the System.identityHashCode() for each of these three instances and use the result to make my new

How to check if a specific key is present in a hash or not?

北城余情 提交于 2019-12-02 13:48:59
I want to check whether the "user" key is present or not in the session hash. How can I do this? Note that I don't want to check whether the key's value is nil or not. I just want to check whether the "user" key is present. Hash 's key? method tells you whether a given key is present or not. session.key?("user") Bozhidar Batsov While Hash#has_key? gets the job done, as Matz notes here , it has been deprecated in favour of Hash#key? . hash.key?(some_key) installero In latest Ruby versions Hash instance has a key? method: {a: 1}.key?(:a) => true Be sure to use the symbol key or a string key

Does foreach() work for non-numerical array keys?

与世无争的帅哥 提交于 2019-12-02 13:24:44
I was wondering if foreach() works when the array looks like this: arr_name[eggs] = something arr_name[pencil] = something else Will foreach work if run as: foreach(arr_name as $key => $value) for they keys that have a non-numerical value ? Yes, foreach supports any kind of key. In your case, $key will be a string, 'eggs' and 'pencil' respectively for each item. In fact, foreach was intended for use with arrays that have non-numerical keys which you can't easily iterate using for . Yes, PHP has no real distinction between arrays with numeric vs non-numeric keys. They're all simply arrays as

PHP CSV to Associative Array with Row Headings

岁酱吖の 提交于 2019-12-02 13:18:13
I have a CSV file with headings in both the columns and rows that i need as an associative array and the examples i have tried on SO haven't worked for me. The closest example i found was CSV to Associative Array but i couldn't print the data, so i must be missing something. Any help would be appreciated $all_rows = array(); $header = 1;//null; // has header if (($handle = fopen($csv_file, "r")) !== FALSE) { while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($header === null) { $header = $row; continue; } $all_rows[] = array_combine($header, $row); } fclose($handle); } // CSV Example

bash associative array key string with colon is giving error

橙三吉。 提交于 2019-12-02 12:33:40
I am creating an associative array of source and destination MAC addresses. $ declare -a SrcDstMap $ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff" -bash: 9c: value too great for base (error token is "9c") $ SrcDstMap["fc:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff" -bash: fc:4e:20:73:e2:72: syntax error in expression (error token is ":4e:20:73:e2:72") How can I tell bash that the given key is a whole string. That's not an associative array. You need to use declare -A , not declare -a . $ declare -A SrcDstMap $ SrcDstMap["9c:4e:20:73:e2:72"]="ff:ff:ff:ff:ff:ff" $ declare -p SrcDstMap declare -A

Create Associative array in Javascript

我与影子孤独终老i 提交于 2019-12-02 10:54:22
问题 I have object which contains array and I want it contains the associative array so keys are strings. How to do it? This does not work: {profiles: { easy:["example" : 1],advanced:["example" : 1] } I wanted to avoid console.log({profiles: { easy:{"example" : 1},advanced:{"example" : 1} }) Because that easy and advanced members are not displayed as array but as object. Another way I know is this: array['key2']='new value'; but that is not part of the object so I would need to split command for