VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-access-and-print-matrix-element-at-specific-position-in-php/

⇱ How to Access and Print Matrix Element at Specific Position in PHP ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Access and Print Matrix Element at Specific Position in PHP ?

Last Updated : 23 Jul, 2025

Accessing and printing elements at specific positions within a matrix is a fundamental operation in PHP when dealing with two-dimensional arrays. A matrix in PHP can be represented as an array of arrays, where each sub-array represents a row in the matrix. This article explores various approaches to accessing and printing a matrix element at a specific position, complete with code examples.

Approach 1: Direct Access

The basic method to access a matrix element is by directly referencing its row and column indexes. This method is best used when you know the exact position of the element you want to access.


Output
Element at row 2, column 3: 6

Approach 2: Using Loops

For a more dynamic approach, especially when the position may vary based on some condition or input, loops can be used to iterate through the matrix and access elements.


Output
Element at row 2, column 1: 8

Approach 3: Using Array Functions

PHP's array functions can also facilitate the access of specific elements. Functions like array_column can be particularly useful for extracting a column, after which accessing a specific row is straightforward.


Output
Element at row 3, column 2: 8

Approach 4: Using Array Destructuring

PHP allows array destructuring (or list assignment) to assign values from an array directly to variables. This approach can be useful when you want to access and print elements from specific positions within the matrix in a concise manner.

Example


Output
Element at position (1, 1) is: 5
Element at position (2, 1) is: 8
Comment
Article Tags: