associative-array

Is It Possible to Declare Constant with Associative Array [duplicate]

独自空忆成欢 提交于 2019-12-07 09:52:29
This question already has answers here : PHP Constants Containing Arrays? (21 answers) Closed 2 years ago . I am trying to declare a constant for our office names of each country with associative array. My declaring code is as below: define( "OUR_OFFICE", [ "Japan" => "Tokyo Shibuya Office", "Taiwan" => "Taipei Shilin Office", "Korea" => "Seoul Yongsan Office", "Singapore" => "Singapore Novena Office", "Australia" => "Sydney Darlinghurst Office" ]); However, it just shows message: Warning: Constants may only evaluate to scalar values Is it possible to declare a constant with associative array?

php foreach over an array and assignment of this array

霸气de小男生 提交于 2019-12-07 05:59:29
问题 I want to add a value to an array while foreaching it : foreach ($array as $cell) { if ($cell["type"] == "type_list") { $cell["list"] = $anObject; error_log(print_r($cell, TRUE), 0); } error_log(print_r($array, TRUE), 0); The first printr is ok but the object added disapear when I leave the loop ant print the array. I guess this is a normal behaviour, what is the best way to work around this "feature" ? 回答1: Just call $cell by reference like this: foreach($array as &$cell) {...} And it should

How to find first/second element of associative array when keys are unknown?

非 Y 不嫁゛ 提交于 2019-12-07 04:49:53
问题 In PHP when you have an associative array, e.g.: $groups['paragraph'] = 3 $groups['line'] = 3 what is the syntax to access the first or second element of the array when you don't know the value of the keys ? Is there something like in a C# LINQ statement where you can say: $mostFrequentGroup = $groups->first()? or $mostFrequentGroup = $groups->getElementWithIndex(0)? Or do I have to use a foreach statement and pick them out as I do at the bottom of this code example: //should return

Bash “declare -A” does not work on macOS

不问归期 提交于 2019-12-07 01:09:11
问题 My guess is that Bash is not updated on macOS. When googling update Bash macOS, I keep getting the bug fix patch. Anyway, I need to use associative arrays in macOS Bash where the command: declare -A yields the error: -bash: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] I have Yosemite. 回答1: declare -A (associative arrays) are a bash 4+ feature. The OS X bash is likely 3.X. I don't know that OS X has an official update for bash 4+. brew/etc. might

array_splice() - Numerical Offsets of Associative Arrays

僤鯓⒐⒋嵵緔 提交于 2019-12-06 21:48:34
I'm trying to do something but I can't find any solution, I'm also having some trouble putting it into works so here is a sample code, maybe it'll be enough to demonstrate what I'm aiming for: $input = array ( 'who' => 'me', 'what' => 'car', 'more' => 'car', 'when' => 'today', ); Now, I want to use array_splice() to remove (and return) one element from the array: $spliced = key(array_splice($input, 2, 1)); // I'm only interested in the key... The above will remove and return 1 element (third argument) from $input (first argument), at offset 2 (second argument), so $spliced will hold the value

Validate dicts in Python

痞子三分冷 提交于 2019-12-06 17:06:08
问题 i looking for tool, or examples to/how to validate dictionaries in python. For example, i have dict: test = {'foo' : 'bar', 'nested' : {'foo1' : 'bar1', 'foo2' : 'bar2'} } And now i must validate it. Lets say, value for key foo must be boolean False or non-empty string. Next, if key foo1 have value bar1 , that key foo2 must be int in range 1..10 . I wrote simple function to do this, but this is not what i exactly want. Yea, sure, i can test every single item in dict with if..else , but if

How to initialize a matrix in plsql

血红的双手。 提交于 2019-12-06 14:52:36
I've tried to Map a table with associative arrays , but i can't figure out how to initialize it here is an example : TYPE RecType IS RECORD ( value1 NUMBER, value2 NUMBER, value3 NUMBER ); TYPE TblType IS TABLE OF RecType INDEX BY PLS_INTEGER; TYPE TblOfTblType IS TABLE OF TblType INDEX BY PLS_INTEGER; matrix TblOfTblType; Now when i tried to initialize the matrix like this : FOR i IN matrix.FIRST .. matrix.LAST LOOP FOR j IN matrix (i).FIRST .. matrix (i).LAST LOOP matrix(i)(j) := NULL; END LOOP; END LOOP; It doesn't work !I also tried matrix := TblOfTblType()(); it shows the following error

ECMAScript5 deep copy of object and arrays

可紊 提交于 2019-12-06 07:52:47
问题 I'd hope to find an example code to do a deep copying of objects in ECMAScript5. The copying should be able to clone Nested objects Nested arrays Nested objects in arrays (clone each array item individually) Note: jQuery.extend() does not seem to handle case 3). Also, I'd hope to do this in clean ECMAScript. Quick googling did not bring up any worthy implementations. 回答1: I finally settled to jQuery.extend() as I couldn't find other good implementations http://api.jquery.com/jQuery.extend/

multidimensional array store each list array inside another array

偶尔善良 提交于 2019-12-06 06:59:29
问题 I have nested multidimensional arrays that may be 2 or 3 levels deep. Inside it I may or may not have list arrray. I need to loop over the array: Array ( [0] => Array ( [id] => 1 [name] => cat_name_1 [list] => Array ( [1] => swgdgbdg [2] => xcbcxb ) ) [1] => Array ( [id] => 3 [name] => cat_name_3 [list] => Array ( [0] => Array ( [id] => 1 [name] => cat_name_1 [list] => Array ( [1] => 543h54h [2] => 54hrhhfr2 ) ) [1] => Array ( [id] => 2 [name] => cat_name_2 [list] => Array ( [1] => eherfherh

PHP display associative array in HTML table

冷暖自知 提交于 2019-12-06 05:44:27
Here is my associative array: $req_data1[]=array( 'depart1'=>$_REQUEST['to'], 'd_time1'=>$d_time5, 'stop'=>"", 'leave_stop'=>"", 'arrival1'=>$_REQUEST['from'], 'a_time1'=>$end_time5, 'price1'=>intval($final_price), 'air_line'=>"xxxxx"); Here is my sorting algorithm: foreach ($req_data as $key => $row) { $depart[$key] = $row['depart']; $d_time[$key] = $row['d_time']; $stop[$key] = $row['stop']; $leave_stop[$key] = $row['leave_stop']; $arrival[$key] = $row['arrival']; $a_time[$key] = $row['a_time']; $price[$key] = $row['price']; } array_multisort($price,SORT_ASC, $req_data); I am displaying the