multidimensional-array

PHP: Foreach in a multidimensional array [duplicate]

≡放荡痞女 提交于 2021-01-28 05:23:18
问题 This question already has answers here : How can I access an array/object? (5 answers) Closed 3 years ago . Got an assignment for school to make a multidimensional array. <?php $cars = array( "car1" => array ( "brand" => 'BMW', "license" => '30-KL-PO', "price" => 10000 ), "car2" => array ( "brand" => 'Mercedes', "license" => '51-ZD-ZD', "price" => 20000 ), "car3" => array ( "brand" => 'Maserati', "license" => 'JB-47-02', "price" => 30000 ) ); foreach($carss as $car){ echo $car['car1']['brand'

push an element to each array in a multidimentional array in php?

最后都变了- 提交于 2021-01-28 04:22:52
问题 Here's my array $myArr = array(array('one', 'two'), array('one', 'two')); I would like add an element to every array inside the $myArr . I dont wanna loop over each array inside and add that element. Is there any quicker way to achieve this using array_map or array_walk .. preferably with one line of code ? Result array should be like $myArr = array(array('one', 'two','three'), array('one', 'two','three')); Thanks a bunch Got it, Kudos to @strager $myvar = 'Three'; $myArr = array_map(function

Delete Array Element From Multidimensional Infinity Array

自古美人都是妖i 提交于 2021-01-28 03:57:41
问题 There is a specific element I want to delete. For example, I want to delete the element with id = 76 in the array below. Also, the array can go together indefinitely. The problem here is that I cannot refresh the page because I do instant action with Vue.js. If I can do this, my next question is probably how can I add an element where I want it now :) [ { "id": 6, "place": 100, "header_tr": "KURUMSAL", "header_en": "CORPORATE", "content_tr": "kurumsal", "content_en": "corporate", "visible

php - how to sum a value from 3 dimensional array

好久不见. 提交于 2021-01-28 02:08:02
问题 I have data as below : Array ( [1] => Array ( [A] => Array ( [AA] => 3 [AB] => 5 ) [B] => Array ( [BA] => 2 ) [2] => Array ( [C] => Array ( [CA] => 4 ) [D] => Array ( [DA] => 1 [DB] => 2 ) ) [3] => Array ( [E] => Array ( [EA] => 1 [EB] => 2 [EC] => 3 ) [F] => Array ( [FA] => 0 [FB] => 7 [FC] => 7 ) ) I want to sum the value and this is my expectation : Array( [1] => 10 [2] => 7 [3] => 20 ) Here is my code that I used for summing the value : $total[$country_id][$province_id][$city_id] =

List to two-dimensional Array

大憨熊 提交于 2021-01-28 02:00:24
问题 How would one go about converting a list of integers to a two-dimensional array? List<int> integerList = new List<int>(); integerList.Add(1); integerList.Add(2); ... integerList.Add(250000); int[,] integerArray = new int[500,500]; //fill integerArray with integerList values here Target output should be in rows, filling x from 0-499 then incrementing y by 1 and repeat. integerArray[x,y] 回答1: Try this: int i = 0; foreach(var number in integerList) { integerArray[i % 500, (int)(i / 500)] =

How to create a sub-matrix in numpy

久未见 提交于 2021-01-28 01:12:43
问题 I have a two-dimensional NxM numpy array: a = np.ndarray((N,M), dtype=np.float32) I would like to make a sub-matrix with a selected number of columns and matrices. For each dimension I have as input either a binary vector, or a vector of indices. How can I do this most efficient? Examples a = array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) cols = [True, False, True] rows = [False, False, True, True] cols_i = [0,2] rows_i = [2,3] result = wanted_function(a, cols, rows) or wanted

How can i create multidimensional list in Python?

六月ゝ 毕业季﹏ 提交于 2021-01-28 00:52:38
问题 How can i create a multidimensional list while iterating through a 1d list based upon some condition. I am iterating over a 1d list and whenever i find a '\n' i should append the so created list with a new list, for example, a = [1,2,3,4,5,'\n',6,7,8,9,0,'\n',3,45,6,7,2] so I want it to be as, new_list = [[1,2,3,4],[6,7,8,9,0],[3,45,6,7,2]] how should i do it? Please help def storeData(k): global dataList dlist = [] for y in k: if y != '\n': dlist.append(y) else: break return dlist This is

Java 2d array as table, print out indexes

老子叫甜甜 提交于 2021-01-27 20:55:53
问题 So, basically I need to print out a 2d array as a table and put indexes "around" it. Random rnd = new Random(); int[][] array = new int[5][5]; for (int row = 0; row < array.length; row++) { for (int col = 0; col < array[row].length; col++) { array[row][col] = rnd.nextInt(6); } } for (int row = 0; row < array.length; row++) { for (int col = 0; col < array[row].length; col++) { if (col < 1) { System.out.print(row+" "); System.out.print(" " + array[row][col] + " "); } else { System.out.print(" "

How to count a dynamic HTML form input array with PHP?

放肆的年华 提交于 2021-01-27 17:31:27
问题 I have a button on a page that when a user pushes it, it creates another "Contact" field on the page. The Contact field allows them to add a new contact to their profile. Also, they can click the button as many times as they want, and it will create that many "Contact" fields. The problem though is that I am having a hard time figuring how many "Contact" fileds have been added. Here is some HTML that is generated when the button is clicked: <div class="item"> <label for="in-1v">First Name

Move null elements for each row in 2d array to the end of that row

落花浮王杯 提交于 2021-01-27 14:01:03
问题 Let's say I have a 2d array which looks like this: [[O, X, null, O, O, null, null], [null, null, O, null, null, O, O]] And I want it to look like this: [[O, X, O, O, null, null, null], [O, O, O, null, null, null, null]] I tried this, but it's not working: String[][] a = new String[row][col]; String[][] b = new String[row][col]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (a[i][j] != null) { a[i][j] = b[i][j]; } else { b[i][j] = a[i][j + 1]; } } } 回答1: Some