How to get sub array in PHP?

前端 未结 3 785
时光取名叫无心
时光取名叫无心 2020-12-17 07:56

I want to get an array composed of the first 4 rows of the original array $arr,

how to do it in PHP?

相关标签:
3条回答
  • 2020-12-17 08:05

    Check this out.. this should help

    <?php
    $input = array("a", "b", "c", "d", "e");
    
    $output = array_slice($input, 2);      // returns "c", "d", and "e"
    $output = array_slice($input, -2, 1);  // returns "d"
    $output = array_slice($input, 0, 3);   // returns "a", "b", and "c"
    
    // note the differences in the array keys
    print_r(array_slice($input, 2, -1));
    print_r(array_slice($input, 2, -1, true));
    ?>
    

    From http://docs.php.net/array_slice.

    0 讨论(0)
  • 2020-12-17 08:10

    You need to use array_slice().

    $output = array_slice($arr, 0, 4);
    
    0 讨论(0)
  • 2020-12-17 08:19

    array_slice()

    $subArray = array_slice($arr,0,4);
    
    0 讨论(0)
提交回复
热议问题