I've got an multidimensional array of undefined length that looks like
Array
(
[0] => Array
(
[price] => 75
)
[1] => Array
(
[price] => 90
)
[2] => Array
(
[price] => 95
)
[3] => Array
(
[price] => 130
)
)
How can I get the value of price of the last element in the array?
Cheers
The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements.
A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.
The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order.
In Java, a multi-dimensional array is nothing but an array of arrays. 2D array − A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns − Int[][] myArray = {{10, 20, 30}, {11, 21, 31}, {12, 22, 32} }
Try this : $array
is your input array
$arr = end($array);
echo $arr['price'];
EDIT : And with PHP 5.4 or newer: end($array)['price']
– fab (Comment by fab)
just use this code $arr
is your array.
echo $arr[count($arr) - 1]['price'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With