Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get end value of multidimensional array

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

like image 885
adnan Avatar asked Feb 27 '13 12:02

adnan


People also ask

How do you find the value of multidimensional array?

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.

What is multidimensional array example?

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.

How multidimensional arrays are stored in memory?

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.

What is the multidimensional array in Java?

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} }


2 Answers

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)

like image 132
Prasanth Bendra Avatar answered Oct 23 '22 15:10

Prasanth Bendra


just use this code $arr is your array.

echo $arr[count($arr) - 1]['price'];
like image 42
Yogesh Suthar Avatar answered Oct 23 '22 17:10

Yogesh Suthar