associative-array

Slicing a multi-dimensional PHP array across one of its elements

寵の児 提交于 2019-12-18 11:20:35
问题 Say for example you just queried a database and you recieved this 2D array. $results = array( array('id' => 1, 'name' => 'red' , 'spin' => 1), array('id' => 2, 'name' => 'green', 'spin' => -1), array('id' => 3, 'name' => 'blue' , 'spin' => .5) ); I often find myself writing loops like this. foreach($results as $result) $names[] = $result['name']; My questions is does there exist a way to get this array $names without using a loop? Using callback functions count as using a loop. Here is a more

java and python equivalent of php's foreach($array as $key => $value)

只愿长相守 提交于 2019-12-18 07:42:20
问题 In php, one can handle a list of state names and their abbreviations with an associative array like this: <?php $stateArray = array( "ALABAMA"=>"AL", "ALASKA"=>"AK", // etc... "WYOMING"=>"WY" ); foreach ($stateArray as $stateName => $stateAbbreviation){ print "The abbreviation for $stateName is $stateAbbreviation.\n\n"; } ?> Output (with key order preserved): The abbreviation for ALABAMA is AL. The abbreviation for ALASKA is AK. The abbreviation for WYOMING is WY. EDIT: Note that the order of

How to implement associative array (not dictionary) in Python?

不问归期 提交于 2019-12-18 04:52:31
问题 I trying to print out a dictionary in Python: Dictionary = {"Forename":"Paul","Surname":"Dinh"} for Key,Value in Dictionary.iteritems(): print Key,"=",Value Although the item "Forename" is listed first, but dictionaries in Python seem to be sorted by values, so the result is like this: Surname = Dinh Forename = Paul How to print out these with the same order in code or the order when items are appended in (not sorted by values nor by keys)? 回答1: You can use a list of tuples (or list of lists)

How to implement associative array (not dictionary) in Python?

我只是一个虾纸丫 提交于 2019-12-18 04:52:29
问题 I trying to print out a dictionary in Python: Dictionary = {"Forename":"Paul","Surname":"Dinh"} for Key,Value in Dictionary.iteritems(): print Key,"=",Value Although the item "Forename" is listed first, but dictionaries in Python seem to be sorted by values, so the result is like this: Surname = Dinh Forename = Paul How to print out these with the same order in code or the order when items are appended in (not sorted by values nor by keys)? 回答1: You can use a list of tuples (or list of lists)

MySQLI Prepared Statement: num_rows & fetch_assoc

*爱你&永不变心* 提交于 2019-12-18 04:13:02
问题 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

php array_merge associative arrays

我们两清 提交于 2019-12-18 01:56:06
问题 I'm trying to prepend an item to the beginning of an associative array. I figured the best way to do this is to use array_merge, but I'm having some odd consequences. I get the id and Name of products from a mysql database, and it gets returned as an associative array, like this (not the actual data coming back, but sample data for this question that represents what the data looks like approximately): $products = array (1 => 'Product 1', 42 => 'Product 42', 100 => 'Product 100'); this is

Automatically sorted by values map in Java

馋奶兔 提交于 2019-12-17 10:57:33
问题 I need to have an automatically sorted-by-values map in Java - so that It keeps being sorted at any time while I'm adding new key-value pairs or update the value of an existing key-value pair, or even delete some entry. Please also have in mind that this map is going to be really big (100's of thousands, or even 10's of millions of entries in size). So basically I'm looking for the following functionality: Supposed that we had a class 'SortedByValuesMap' that implements the aforementioned

php: how to get associative array key from numeric index?

守給你的承諾、 提交于 2019-12-17 10:25:27
问题 If I have: $array = array( 'one' =>'value', 'two' => 'value2' ); how do I get the string one back from $array[1] ? 回答1: You don't. Your array doesn't have a key [1] . You could: Make a new array, which contains the keys: $newArray = array_keys($array); echo $newArray[0]; But the value "one" is at $newArray[0] , not [1] . A shortcut would be: echo current(array_keys($array)); Get the first key of the array: reset($array); echo key($array); Get the key corresponding to the value "value": echo

PHP/SQL Insert Error when using Named Placeholders

泄露秘密 提交于 2019-12-17 10:03:35
问题 I have the following PHP PDO statement: $STH = $this->_db->prepare("INSERT INTO UserDetails (FirstName, LastName, Address, City, County, PostCode, Phone, Mobile, Sex, DOB, FundraisingAim, WeeksAim, LengthsAim, HearAboutID, MotivationID, WelcomePackID, ContactPrefID, TitleID) VALUES (:firstName, :lastName, :address, :city, :county, :postCode, :phone, :mobile, :sex, :DOB, :fundraisingAim, :weeksAim, :lengthsAim, :hearAbout, :motivation, :welcomePackPref, :contactPref, :title)"); $STH->execute(

Associative arrays in C

强颜欢笑 提交于 2019-12-17 08:33:22
问题 I am implementing a way to transfer a set of data to a programmable dongle. The dongle is based on a smart card technology and can execute an arbitrary code inside. The input and output data is passed as a binary blocks that can be accessed via input and output pointers. I would like to use an associative array to simplify the data processing code. Everything should work this way: First the host application: // Host application in C++ in_data["method"] = "calc_r"; in_data["id"] = 12; in_data[