How can a PHP class be designed to use an index array for navigation while reading data from a separate data array?

To design a PHP class that uses an index array for navigation while reading data from a separate data array, you can create a class with methods to navigate through the index array and retrieve corresponding data from the data array. The index array can be used to keep track of the current position while the data array stores the actual data. By implementing methods to move to the next or previous index and retrieve data based on the current index, you can effectively navigate and access the data in a structured manner.

class DataNavigator {
    private $indexArray;
    private $dataArray;
    private $currentIndex;

    public function __construct($indexArray, $dataArray) {
        $this->indexArray = $indexArray;
        $this->dataArray = $dataArray;
        $this->currentIndex = 0;
    }

    public function moveToNext() {
        if ($this->currentIndex < count($this->indexArray) - 1) {
            $this->currentIndex++;
        }
    }

    public function moveToPrevious() {
        if ($this->currentIndex > 0) {
            $this->currentIndex--;
        }
    }

    public function getCurrentData() {
        return $this->dataArray[$this->indexArray[$this->currentIndex]];
    }
}

// Example usage
$indexArray = [0, 1, 2];
$dataArray = ['data1', 'data2', 'data3'];
$dataNavigator = new DataNavigator($indexArray, $dataArray);

echo $dataNavigator->getCurrentData(); // Output: data1
$dataNavigator->moveToNext();
echo $dataNavigator->getCurrentData(); // Output: data2
$dataNavigator->moveToNext();
echo $dataNavigator->getCurrentData(); // Output: data3
$dataNavigator->moveToNext();
echo $dataNavigator->getCurrentData(); // Output: data3 (no change beyond last index)