How to access first level keys of a 2d array with a foreach loop? [duplicate]
You can access your array keys like so: foreach ($array as $key => $value)
You can access your array keys like so: foreach ($array as $key => $value)
If you have a=[[1,1],[2,1],[3,1]] b=[[1,2],[2,2],[3,2]] Then a[1][1] Will work fine. It points to the second column, second row just like you wanted. I’m not sure what you did wrong. To multiply the cells in the third column you can just do c = [a[2][i] * b[2][i] for i in range(len(a[2]))] Which will work for any … Read more
I’d go for the first option (the single 1D array) as it will give you a single block of memory to play in rather than potentially thousands of fragmented memory blocks If accessing the correct element of the array is doing your head in though, I’d write a utility method to convert x, y, z … Read more
It’s the array that’s causing trouble in: void print_graph(g_node graph_node[], double weight[][], int nodes); The second and subsequent dimensions must be given: void print_graph(g_node graph_node[], double weight[][32], int nodes); Or you can just give a pointer to pointer: void print_graph(g_node graph_node[], double **weight, int nodes); However, although they look similar, those are very different internally. … Read more
No. You could of course write a wrapper class that represents a slice, and has an indexer internally – but nothing inbuilt. The other approach would be to write a method that makes a copy of a slice and hands back a vector – it depends whether you want a copy or not. using System; … Read more
There are some problems with 2 dimensional Arrays the way you implement them. a= [[1,2],[3,4]] a[0][2]= 5 # works a[2][0]= 6 # error Hash as Array I prefer to use Hashes for multi dimensional Arrays a= Hash.new a[[1,2]]= 23 a[[5,6]]= 42 This has the advantage, that you don’t have to manually create columns or rows. … Read more
Convert arrays to a format, where array index is the sight_id: $b1 =array(); foreach($a1 as $x) $b1[$x[‘sight_id’]] = $x[‘location’]; $b2 =array(); foreach($a2 as $x) $b2[$x[‘sight_id’]] = $x[‘location’]; Calculate the differences and intersection: $c_intersect = array_intersect_key($b1,$b2); $c_1 = array_diff_key($b1,$b2); $c_2 = array_diff_key($b2,$b1); Convert arrays back to your format: $intersect_array = array(); foreach($c_intersect as $i=>$v) $intersect_array[] = … Read more
Personally, I would use a custom (anonymous) function in conjunction with usort(). EDIT: Re – your comment. Hopefully this will put you on the right track. This function gives equal priority to elements which both have EN or neither have EN, or adjusted priority when just one has EN. usort($array,function ($a, $b) { $ac = … Read more
arr = [[7,3], [7,3], [3,8], [7,3], [7,3], [1,2]]; function multiDimensionalUnique(arr) { var uniques = []; var itemsFound = {}; for(var i = 0, l = arr.length; i < l; i++) { var stringified = JSON.stringify(arr[i]); if(itemsFound[stringified]) { continue; } uniques.push(arr[i]); itemsFound[stringified] = true; } return uniques; } multiDimensionalUnique(arr); Explaination: Like you had mentioned, the other … Read more
If you mean a jagged array (T[][]), SelectMany is your friend. If, however, you mean a rectangular array (T[,]), then you can just enumerate the date data via foreach – or: int[,] from = new int[,] {{1,2},{3,4},{5,6}}; int[] to = from.Cast<int>().ToArray();