我有一个名为$results
的数组,看起来像这样:
[41] => Array
(
[id] => 200
[0] => 200
[description] => Exercitationem quidem in soluta repellendus. Nihil rem eius doloribus error qui consequatur rerum. Ut ea et reprehenderit ea.
[1] => Exercitationem quidem in soluta repellendus. Nihil rem eius doloribus error qui consequatur rerum. Ut ea et reprehenderit ea.
[amount] => 2696.00
[2] => 2696.00
[event_class] => 6
[3] => 6
[datetime] => 1978-11-19 14:13:20
[4] => 1978-11-19 14:13:20
)
[42] => Array
(
[id] => 201
[0] => 201
[description] => Cupiditate repudiandae aliquid et aut vitae ipsum esse. Odit id debitis atque. Fugiat et dolores tempore officiis.
[1] => Cupiditate repudiandae aliquid et aut vitae ipsum esse. Odit id debitis atque. Fugiat et dolores tempore officiis.
[amount] => 23.00
[2] => 23.00
[event_class] => 3
[3] => 3
[datetime] => 2004-02-23 00:30:56
[4] => 2004-02-23 00:30:56
)
使用以下代码,我尝试从此数组的特定部分提取几个连续记录,并将它们移动到名为$fields
的新数组。这是为了分页。
// based off the desired page, calculate the lowest ID for the record that should be shown
$lower_bound = (int)($_GET['page'] * 10) - 10;
// calculate the highest ID to be shown
$upper_bound = (int)$lower_bound + 10;
$fields = array();
for($i=$lower_bound; $i<=$upper_bound; $i++) {
$fields = array_push($results[$i]);
}
echo '<pre>';
print_r($fields);
echo '</pre>';
但是,print_r($fields);
什么都不返回。有人可以建议为什么会这样吗?
我知道array_slice()
函数存在,但我需要通过上述方法解决问题。
答案 0 :(得分:2)
我还没有永远使用array_push()
,而你却没有正确使用它。它需要两个参数,一个数组和推送到数组的值。但是,只需使用:
for($i=$lower_bound; $i<=$upper_bound; $i++) {
$fields[] = $results[$i];
}
话虽如此,这更简单:
$fields = array_slice($results, $lower_bound, 10);