associative-array

How can I put the results of a MySQLi prepared statement into an associative array?

青春壹個敷衍的年華 提交于 2019-11-27 08:08:28
I have a sql query and a mysqli prepared statement: $sql = 'SELECT photographers.photographer_id, photographers.photographer_name FROM photographers'; $stmt = $conn->stmt_init(); if ($stmt->prepare($sql)) { $stmt->bind_result($photographer_id, $photographer_name); $OK = $stmt->execute(); $stmt->fetch(); } How can I store the results in an associative array so I can loop it later and get to all the data returned by the sql string? Chris Try the following: $meta = $statement->result_metadata(); while ($field = $meta->fetch_field()) { $params[] = &$row[$field->name]; } call_user_func_array(array(

Display array values in PHP

江枫思渺然 提交于 2019-11-27 07:58:38
So, I'm working with PHP for the first time and I am trying to retrieve and display the values of an array. After a lot of googling, the only methods I can find for this are print_r , var_dump or var_export . However, all of these methods return something that looks like this: [a] => apple [b] => banana [c] => orange I can't figure out how to style this outout. I need to strip away the [a] => part and add commas. I know this must be a pretty straightforward process but I haven't been able to track down any documentation that demonstrates how to do it. Shakti Singh There is foreach loop in php.

PHP: Get n-th item of an associative array

半腔热情 提交于 2019-11-27 07:57:02
If you have an associative array: Array ( [uid] => Marvelous [status] => 1 [set_later] => Array ( [0] => 1 [1] => 0 ) [op] => Submit [submit] => Submit ) And you want to access the 2nd item, how would you do it? $arr[1] doesn't seem to be working: foreach ($form_state['values']['set_later'] as $fieldKey => $setLater) { if (! $setLater) { $valueForAll = $form_state['values'][$fieldKey]; $_SESSION[SET_NOW_KEY][array_search($valueForAll, $form_state['values'])] = $valueForAll; // this isn't getting the value properly } } This code is supposed to produce: $_SESSION[SET_NOW_KEY]['status'] = 1 But

How to pass an associative array as argument to a function in Bash?

眉间皱痕 提交于 2019-11-27 07:25:18
How do you pass an associative array as an argument to a function? Is this possible in Bash? The code below is not working as expected: function iterateArray { local ADATA="${@}" # associative array for key in "${!ADATA[@]}" do echo "key - ${key}" echo "value: ${ADATA[$key]}" done } Passing associative arrays to a function like normal arrays does not work: iterateArray "$A_DATA" or iterateArray "$A_DATA[@]" Florian Feldhaus I had exactly the same problem last week and thought about it for quite a while. It seems, that associative arrays can't be serialized or copied. There's a good Bash FAQ

How to POST an associative array in PHP

自闭症网瘾萝莉.ら 提交于 2019-11-27 06:50:07
问题 I have the following form: <form action="options.php" method="post"> <input type="text" name="deptid" id="deptid" /> <input type="text" name="deptname" id="deptname" /> <input type="submit" name="submit" id="submit" value="save" /> </form> EDIT Is it possible to pass the two values into one associative array BEFORE submission ? I would like to pass it in this form: array('deptid'=>'deptname') I need this because I avoid to modify the script of the destination php file(options.php) Thanks. 回答1

Associative arrays in C

99封情书 提交于 2019-11-27 06:48:25
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["loc_a"] = 56.19; in_data["loc_l"] = 44.02; processor->send(in_data); Next the code inside the dongle:

Prevent json_encode associative array sorting [duplicate]

风格不统一 提交于 2019-11-27 06:48:05
问题 This question already has an answer here: json_encode not preserving order 2 answers I have a associative array Array( [289] => Array( 'name'=> 'One' ), [292] => Array( 'name'=> 'One' ), [290] => Array( 'name'=> 'One' ) ) After i use json_encode on this array. The keys are sorted, although i get it as JSON object. Is there way to prevent this behaviour? 回答1: there is no standard that says it has to be in a certain order. See this for a related question: How do you stop Chrome and Opera

PHP prepend associative array with literal keys?

╄→гoц情女王★ 提交于 2019-11-27 06:46:37
Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I'm hoping for something that will work with literal keys. As an example I'd like to do the following: $array1 = array('fruit3'=>'apple', 'fruit4'=>'orange'); $array2 = array('fruit1'=>'cherry', 'fruit2'=>'blueberry'); // prepend magic $resulting_array = ('fruit1'=>'cherry', 'fruit2'=>'blueberry', 'fruit3'=>'apple', 'fruit4'=>'orange'); Can't you just do: $resulting_array = $array2 + $array1; ? The answer is no. You cannot prepend an associative array with a

Change key in associative array in PHP

戏子无情 提交于 2019-11-27 06:43:36
问题 Say I have an array like this: array(2) { [0]=> array(2) { ["n"]=> string(4) "john" ["l"]=> string(3) "red" } [1]=> array(2) { ["n"]=> string(5) "nicel" ["l"]=> string(4) "blue" } } How would I change the keys of the inside arrays? Say, I want to change "n" for "name" and "l" for "last_name". Taking into account that it can happen than an array doesn't have a particular key. 回答1: Using array_walk array_walk($array, function (& $item) { $item['new_key'] = $item['old_key']; unset($item['old_key

How can I merge consecutive subArrays which have the same data in it?

前提是你 提交于 2019-11-27 06:10:46
问题 I have this array: $opening_hours = array( 'Monday' => array('09:00', '17:00'), 'Tuesday' => array('09:00', '17:00'), 'Wednesday' => array('08:00', '13:00'), 'Thursday' => array('09:00', '17:00'), 'Friday' => array('09:00', '17:00'), 'Saturday' => array('10:00', '16:00'), 'Sunday' => array('Closed'), ); I need to somehow merge those opening hours to the array which should look like this: $merged_opening_hours = array( 'Monday - Tuesday' => array('09:00', '17:00'), 'Wednesday' => array('08:00'