multidimensional-array

Traversing a multi-dimensional hash in Perl

百般思念 提交于 2019-12-30 04:57:06
问题 If you have a hash (or reference to a hash) in perl with many dimensions and you want to iterate across all values, what's the best way to do it. In other words, if we have $f->{$x}{$y}, I want something like foreach ($x, $y) (deep_keys %{$f}) { } instead of foreach $x (keys %f) { foreach $y (keys %{$f->{$x}) { } } 回答1: Here's an option. This works for arbitrarily deep hashes: sub deep_keys_foreach { my ($hashref, $code, $args) = @_; while (my ($k, $v) = each(%$hashref)) { my @newargs =

PHP Search an Array for multiple key / value pairs

余生长醉 提交于 2019-12-30 04:05:27
问题 I have a array list (for this example I'm using cell phones). I'm wanting to be able to search for multiple key/value pairs and return it's parent array index. For example, here is my array: // $list_of_phones (array) Array ( [0] => Array ( [Manufacturer] => Apple [Model] => iPhone 3G 8GB [Carrier] => AT&T ) [1] => Array ( [Manufacturer] => Motorola [Model] => Droid X2 [Carrier] => Verizon ) ) I'm wanting to be able to do something like the following: // This is not a real function, just used

PHP convert one dimensional array into multidimensional

依然范特西╮ 提交于 2019-12-30 03:33:16
问题 I have one array as $tmpArr = array('A', 'B', 'C'); I want to process this array and want new array as $tmpArr[A][B][C] = C I.e last element becomes the value of final array. Can anyone suggest the solution? Please help. Thanks in advance 回答1: Iterate the array of keys and use a reference for the end of the chain: $arr = array(); $ref = &$arr; foreach ($tmpArr as $key) { $ref[$key] = array(); $ref = &$ref[$key]; } $ref = $key; $tmpArr = $arr; 回答2: $tmpArr = array('A', 'B', 'C'); $array =

Using scipy.stats.gaussian_kde with 2 dimensional data

吃可爱长大的小学妹 提交于 2019-12-30 03:22:06
问题 I'm trying to use the scipy.stats.gaussian_kde class to smooth out some discrete data collected with latitude and longitude information, so it shows up as somewhat similar to a contour map in the end, where the high densities are the peak and low densities are the valley. I'm having a hard time putting a two-dimensional dataset into the gaussian_kde class. I've played around to figure out how it works with 1 dimensional data, so I thought 2 dimensional would be something along the lines of:

array_filter with assoc array?

可紊 提交于 2019-12-30 02:27:11
问题 I am using array_filter to do something like this: function endswithy($value) { return (substr($value, -1) == 'y'); } $people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe"); $withy = array_filter($people, "endswithy"); var_dump($withy); BUT with the more option in filter for example $people = array( "Johnny"=>array("year"=>1989, "job"=>"prof"), "Timmy"=>array("year"=>1989, "job"=>"std"), "Bobby"=>array("year"=>1988), "Sam"=>array("year"=>1983), "Tammy"=>array("year"=>1985

array_filter with assoc array?

旧巷老猫 提交于 2019-12-30 02:27:07
问题 I am using array_filter to do something like this: function endswithy($value) { return (substr($value, -1) == 'y'); } $people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe"); $withy = array_filter($people, "endswithy"); var_dump($withy); BUT with the more option in filter for example $people = array( "Johnny"=>array("year"=>1989, "job"=>"prof"), "Timmy"=>array("year"=>1989, "job"=>"std"), "Bobby"=>array("year"=>1988), "Sam"=>array("year"=>1983), "Tammy"=>array("year"=>1985

Please explain the difference

坚强是说给别人听的谎言 提交于 2019-12-30 02:26:08
问题 i have a program about 2-D arrays base adress is 8678 #include<stdio.h> #include<conio.h> main() { int arr[3][3]={ {83,8,43}, {73,45,6}, {34,67,9} }; printf("%d ",&arr+1); //points to 8696 printf("%d ",arr+1); //points to 8684 return 0; } what is the difference between arr+1 and &arr+1 ? 回答1: Well, they're different things. arr decays in most contexts to a pointer to the first element of your array - that means a pointer to the first 3-element row in your 2D array: type int (*)[3] . arr + 1 ,

How to split an array based on a certain value?

 ̄綄美尐妖づ 提交于 2019-12-29 09:27:21
问题 I currently have an array of location based information similar to below (which is already sorted by time). The first key/value is event which represents the start of a journey (00), a running journey (01) and an end of a journey (10). Array ( [0] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 ) [1] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 ) [2] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] =>

Allocate memory for 2D array with C++ new[]

对着背影说爱祢 提交于 2019-12-29 09:17:08
问题 When I read some values from the user and I need to create an array of the specific size I do it somehow like this: #include <iostream> using namespace std; unsigned* numbers; int main() { int a; cin >> a; numbers = new unsigned[a]; } How can I do it with a 2d array(sized a*b read from the user)? 回答1: Anything that would look like a 2D-Array in code will not be a physical 2D array in memory, but either one plain block of memory or scattered, depending on how you allocate it. You can torture

Javascript : How group and sum values from multidimensional array

a 夏天 提交于 2019-12-29 09:09:31
问题 I have an array like this: I would like to group and get the sum of each repetition like this: AGE270: 9 AGE203: 5 AGE208: 9 ... AGEXXX: n 回答1: Simple solution using Array.prototype.reduce function: // Replace arr with your actual array! var arr = [ { AGENDADOR: 'AGE270', TOTAL : 6}, { AGENDADOR: 'AGE270', TOTAL : 3}, { AGENDADOR: 'AGE203', TOTAL : 5}, { AGENDADOR: 'AGE028', TOTAL : 9}, ], totals = arr.reduce(function (r, o) { (r[o.AGENDADOR])? r[o.AGENDADOR] += o.TOTAL : r[o.AGENDADOR] = o