PHP选择n个数组

时间:2014-10-18 14:52:39

标签: php arrays

我正面临着这个问题,我似乎无法想出办法。我有这样的数组,

    $array = [(
    "name" => "marks", "score" => 24),
    ("name" => "arsh", "score"=>54),
    ("name" => "jack" , "score" =>32),
    ("name" => "welch" , "score" =>34),
    ("name" => "sid" , "score" =>32)];

如何选择前n个数组,比如说我需要前2个数组。因此,如何从这些数据中得到这样的输出,

    $array = [(
    "name" => "marks", "score" => 24),
    ("name" => "arsh", "score"=>54)]

由于

1 个答案:

答案 0 :(得分:1)

array_slice应该这样做:

$firstTwo = array_slice ($array, 0, 2);