Loop a multidimensional array and only print two specific column values per row

Using a foreach loop without a key: foreach($array as $item) { echo $item[‘filename’]; echo $item[‘filepath’]; // To know what’s in $item echo ‘<pre>’; var_dump($item); } Using a foreach loop with a key: foreach($array as $i => $item) { echo $array[$i][‘filename’]; echo $array[$i][‘filepath’]; // $array[$i] is same as $item } Using a for loop: for ($i … Read more

Differences between a multidimensional array “[,]” and an array of arrays “[][]” in C#?

Array of arrays (jagged arrays) are faster than multi-dimensional arrays and can be used more effectively. Multidimensional arrays have nicer syntax. If you write some simple code using jagged and multidimensional arrays and then inspect the compiled assembly with an IL disassembler you will see that the storage and retrieval from jagged (or single dimensional) … Read more

Sort an array of associative arrays by column value

You are right, the function you’re looking for is array_multisort(). Here’s an example taken straight from the manual and adapted to your case: $price = array(); foreach ($inventory as $key => $row) { $price[$key] = $row[‘price’]; } array_multisort($price, SORT_DESC, $inventory); As of PHP 5.5.0 you can use array_column() instead of that foreach: $price = array_column($inventory, … Read more

How to create a Multidimensional ArrayList in Java?

ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>(); Depending on your requirements, you might use a Generic class like the one below to make access easier: import java.util.ArrayList; class TwoDimentionalArrayList<T> extends ArrayList<ArrayList<T>> { public void addToInnerArray(int index, T element) { while (index >= this.size()) { this.add(new ArrayList<T>()); } this.get(index).add(element); } public void addToInnerArray(int index, int index2, T element) … Read more

Transforming a row vector into a column vector in Numpy

you can use the transpose operation to do this: Example: In [2]: a = np.array([[1,2], [3,4], [5,6]]) In [5]: a.shape Out[5]: (3, 2) In [6]: a_trans = a.T #or: np.transpose(a), a.transpose() In [8]: a_trans.shape Out[8]: (2, 3) In [7]: a_trans Out[7]: array([[1, 3, 5], [2, 4, 6]]) Note that the original array a will still … Read more