Current Location: Home> Latest Articles> Use array_slice to return result mock data using the API

Use array_slice to return result mock data using the API

gitbox 2025-05-26

array_slice is a built-in array operation function in PHP. Its function is to extract a part of the array elements and return a new array based on the specified starting position and length from an array. Its basic syntax is as follows:

 array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
  • $array : the original array to operate;

  • $offset : Start position, support negative numbers (counting starts from the end of the array);

  • $length : intercepts the length, defaults to the end of the array;

  • $preserve_keys : Whether to retain the key name of the original array, the default is false .

How to generate mock data using array_slice?

Suppose we already have a complete simulation data set, such as an array of user lists, and now we want to achieve the pagination effect, and the simulation backend API returns part of the data. With array_slice , you can easily intercept the required data fragments by simply specifying the starting index of the page and the number of pages per page.

Examples are as follows:

 <?php

// Completely simulated user data
$users = [
    ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Bob', 'email' => '[email protected]'],
    ['id' => 3, 'name' => 'Charlie', 'email' => '[email protected]'],
    ['id' => 4, 'name' => 'David', 'email' => '[email protected]'],
    ['id' => 5, 'name' => 'Eve', 'email' => '[email protected]'],
    ['id' => 6, 'name' => 'Frank', 'email' => '[email protected]'],
];

// Simulate paging parameters,Assume the current page and the number of bars per page
$page = 2;
$pageSize = 2;

// Calculate the starting index
$offset = ($page - 1) * $pageSize;

// use array_slice Intercept data
$pageData = array_slice($users, $offset, $pageSize);

// Generate simulation API Return to format
$response = [
    'code' => 0,
    'msg' => 'success',
    'data' => $pageData,
];

// Output JSON
header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);

In this example, array_slice calculates the starting position according to the current page number, intercepts the corresponding amount of user data from the $users array, and constructs a JSON format that meets the interface return specification, which facilitates front-end call and debugging.

The actual scenario of combining URLs

In actual projects, the interface may contain pagination requests based on URL parameters, such as:

 https://api.gitbox.net/users?page=2&pageSize=3

We can dynamically control the returned data slice through PHP :

 <?php
// Assume that there is complete data
$users = [/* The same data above */];

// Get the paging parameters,Set default values
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$pageSize = isset($_GET['pageSize']) ? max(1, intval($_GET['pageSize'])) : 3;

$offset = ($page - 1) * $pageSize;

$pageData = array_slice($users, $offset, $pageSize);

$response = [
    'code' => 0,
    'msg' => 'success',
    'data' => $pageData,
];

header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);

In this way, paging simulation can be implemented using simple and efficient array_slice to meet the front-end interface testing needs.