简体   繁体   中英

How to get last value in multidimensional array in php?

I need to take every 10th value of this array? Kindly help me to this questions?

array (
[0] => array
    (
        [0] => 88%
        [1] => 88%
        [2] => 88%
        [3] => 88%
        [4] => 88%
        [5] => 88%
        [6] => 88%
        [7] => 88%
        [8] => 88%
        [9] => 88%
        [10] => 88%
    )
[1] => Array
    (
        [0] => 401 MByte
        [1] => 401 MByte
        [2] => 401 MByte
        [3] => 401 MByte
        [4] => 401 MByte
        [5] => 401 MByte
        [6] => 401 MByte
        [7] => 401 MByte
        [8] => 401 MByte
        [9] => 401 MByte
        [10] => 401 MByte
    )
[2] => Array
    (
        [0] => 452,947 MByte
        [1] => 451,651 MByte
        [2] => 450,626 MByte
        [3] => 451,137 MByte
        [4] => 452,628 MByte
        [5] => 454,383 MByte
        [6] => 456,065 MByte
        [7] => 454,829 MByte
        [8] => 453,094 MByte
        [9] => 451,930 MByte
        [10] => 451,923 MByte
    )
)

use end() function.

foreach( $yourArry as $one ) {
    echo end($one) ;
}

Here's how you could get first ten entries of each array

$arr = array_map( function($v) { 
            return array_slice($v, 0, 9);
       }, $arr);
var_dump($arr);

This code may help.

$myArray = array (
[0] => array
    (
        [0] => 88%
        [1] => 88%
        [2] => 88%
        [3] => 88%
        [4] => 88%
        [5] => 88%
        [6] => 88%
        [7] => 88%
        [8] => 88%
        [9] => 88%
        [10] => 88%
    )
[1] => Array
    (
        [0] => 401 MByte
        [1] => 401 MByte
        [2] => 401 MByte
        [3] => 401 MByte
        [4] => 401 MByte
        [5] => 401 MByte
        [6] => 401 MByte
        [7] => 401 MByte
        [8] => 401 MByte
        [9] => 401 MByte
        [10] => 401 MByte
    )
[2] => Array
    (
        [0] => 452,947 MByte
        [1] => 451,651 MByte
        [2] => 450,626 MByte
        [3] => 451,137 MByte
        [4] => 452,628 MByte
        [5] => 454,383 MByte
        [6] => 456,065 MByte
        [7] => 454,829 MByte
        [8] => 453,094 MByte
        [9] => 451,930 MByte
        [10] => 451,923 MByte
    )
);

$my_values = array();
foreach($myArray as $a) {
    if(isset($a[10]))
        $my_values[] = $a[10];
}

var_dump($my_values); //to check the values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM