首页 > 解决方案 > 如何从关联 PHP 数组中获取 First to End 项?

问题描述

如果我有一个像这样的数组:

$fuel[0]->fuel_qty = 1.00
$fuel[1]->fuel_qty = 2.00

以下函数获取数组的第一个元素 (1.00)。

public function printFuel($id) {
    $fuel = $this->Fuel_model->directFuelQtyById($id);      
    $this->amount_word = $this->convert_number_to_words($fuel[0]->fuel_qty) . '<br>'." Only";
}

我想通过修改这个函数来获取所有的数组元素(1.00、2.00 等)。我怎么能得到这个。谁能帮我?

标签: phpcodeigniter

解决方案


要获取最后一个元素,请使用 array_pop:https ://www.php.net/manual/en/function.array-pop.php

要获取第一个元素,请使用 array_shift:https ://www.php.net/manual/en/function.array-shift.php


推荐阅读