associative-array

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

前提是你 提交于 2019-11-28 11:15:19
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', '13:00'), 'Thursday - Friday' => array('09:00', '17:00'), 'Saturday' => array('10:00', '16:00');

How to insert a new key and value in multidimensional array?

好久不见. 提交于 2019-11-28 08:45:22
Following is the output of my multidimensional array $csmap_data Array ( [0] => Array ( [cs_map_id] => 84 [cs_subject_id] => 1 ) [1] => Array ( [cs_map_id] => 85 [cs_subject_id] => 5 ) [flag] => 1 ) Initially there was no [flag] => 1 key-value in the array, I added it to the array $csmap_data . But I want to add the [flag] => 1 in the above two array elements, not as a separate array element. In short I wanted following output : Array ( [0] => Array ( [cs_map_id] => 84 [cs_subject_id] => 1 [flag] => 1 ) [1] => Array ( [cs_map_id] => 85 [cs_subject_id] => 5 [flag] => 1 ) ) The code I was trying

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

谁说我不能喝 提交于 2019-11-28 07:26: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

What's the difference between pls_integer and binary_integer?

自作多情 提交于 2019-11-28 06:48:48
I've inherited some code which is going to be the base for some additional work. Looking at the stored procs, I see quite a lot of associative-arrays. Some of these are indexed by binary_integers, some by pls_integers. Are there any differences between the two? I had a look at the documentation , but apart from this line: The PL/SQL data types PLS_INTEGER and BINARY_INTEGER are identical. For simplicity, this document uses PLS_INTEGER to mean both PLS_INTEGER and BINARY_INTEGER . I couldn't find any difference between the two. So what's the difference? Are both around for historical

convert array to associative array

六月ゝ 毕业季﹏ 提交于 2019-11-28 05:22:00
问题 I have an array like this: Array ( [0] => name [1] => john [2] => last [3] => doe [4] => company [5] => sony ) I need to convert to this: Array ( [name] => john [last] => doe [company] => sony ) Any ideas? 回答1: for ($i = 0; $i < count($myArray); $i += 2) $newArray[ $myArray[$i] ] = $myArray[$i+1]; 来源: https://stackoverflow.com/questions/15673232/convert-array-to-associative-array

Fastest way to implode an associative array with keys

北城以北 提交于 2019-11-28 04:36:17
I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use ' & ' for xhtml links or ' & ' otherwise. My first inclination is to use foreach but since my method could be called many times in one request I fear it might be too slow. <?php $Amp = $IsXhtml ? '&' : '&'; $Parameters = array('Action' => 'ShowList', 'Page' => '2'); $QueryString = ''; foreach ($Parameters as $Key => $Value) $QueryString .= $Amp . $Key . '=' . $Value; Is there a faster way? Greg You can use http_build_query() to

How to add an array value to the middle of an associative array?

浪尽此生 提交于 2019-11-28 04:32:56
Lets say I have this array: $array = array('a'=>1,'z'=>2,'d'=>4); Later in the script, I want to add the value 'c'=>3 before 'z' . How can I do this? Yes, the order is important. When I run a foreach() through the array, I do NOT want this newly added value added to the end of the array. I am getting this array from a mysql_fetch_assoc() The keys I used above are placeholders. Using ksort() will not achieve what I want. http://www.php.net/manual/en/function.array-splice.php#88896 accomplishes what I'm looking for but I'm looking for something simpler. Take a sample db table with about 30

Value objects vs associative arrays in PHP

荒凉一梦 提交于 2019-11-28 04:30:17
(This question uses PHP as context but isn't restricted to PHP only. e.g. Any language with built in hash is also relevant) Let's look at this example (PHP): function makeAFredUsingAssoc() { return array( 'id'=>1337, 'height'=>137, 'name'=>"Green Fred"); } Versus: class Fred { public $id; public $height; public $name; public function __construct($id, $height, $name) { $this->id = $id; $this->height = $height; $this->name = $name; } } function makeAFredUsingValueObject() { return new Fred(1337, 137, "Green Fred"); } Method #1 is of course terser, however it may easily lead to error such as

Dynamically creating/inserting into an associative array in PHP

心不动则不痛 提交于 2019-11-28 04:26:55
问题 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

Hash tables VS associative arrays

孤者浪人 提交于 2019-11-28 03:26:09
Recently I have read about hash-tables in a very famous book " Introduction to Algorithms ". I haven't used them in any real applications yet, but want to. But I don't know how to start. Can anyone give me some samples of using it, for example, how to realize a dictionary application (like ABBYY Lingvo) using hash-tables? And finally I would like to know what is the difference between hash-tables and associative arrays in PHP, I mean which technology should I use and in which situations? If I am wrong (I beg pardon) please correct me, because actually I am starting with hash-tables and I have