Working with PHP Array – Indexed, Associative & Multi-Dimensional
October 11, 2016 Comments 0 313 Views

Working with PHP Array – Indexed, Associative & Multi-Dimensional

PHP Array are variables which can hold more than one value at a time. For example if you want to store 1000 numbers then instead of defining 1000 variables its easy to define an array of 1000 length. Three different kind of arrays are available and each array value is accessed using array index.

Following are the types of php array :

  • Indexed array − Array with a numeric index. Values are stored and accessed in linear fashion.
  • Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array − A multidimensional array is an array containing one or more arrays. The dimension of an array indicates the number of indices you need to select an element. For a two-dimensional array you need two indices to select an element. For a three-dimensional array you need three indices to select an element.

Indexed Array :

Indexed arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

Following is the example showing how to create & access numeric arrays. Here we have used array() function to create array. This function is explained in function reference.

Associative Arrays :

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the marks of students in an array, a numerically indexed array would not be the best choice. Instead, we could use the student names as the keys in associative array, and the value would be their marks.

Example :

Multidimensional Arrays :

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example :

Previous Remove category & tag base from WordPress url
Next Magento 2 Indexing Management Using Magento CLI Command

About author

Rio
Rio 37 posts

Expert web developer working in PHP, Wordpress, Joomla, Magento, Javascript etc.

You might also like

Post data to third party site using PHP CURL

PHP supports CURL library which allows you to connect and communicate to many different types of servers with many different types of protocols and supports the http, https, ftp etc.

How to detect website is access from Mobile using PHP.

Todays trend shows that maximum number of website access using mobile is increased. People are using smart phone to access the mobile instead of their desktop OR laptop. Because mobile

Error Handling in PHP 7

The next feature we going to cover are the changes to Error Handling. Handling fatal errors in the past has been next to impossible in PHP. A fatal error would

ZIP Files & Folder using Simple PHP Code

Using PHP you can Zip Files and Folders with a simple php code, which will provide you with the functionality to create zip automatically without manual efforts. Simple PHP code to

Difference between echo and print statements

In PHP there are two basic ways to get your output : echo and print statements. We will show the difference between echo and print statements with a working example.

Secure directory by allowing access to IP address & Deny all using htaccess

Your website may have folders which you want to keep secure access using URL. For example Admin area you want to keep secure to access using particular IP address and

Creating HTML Form with PHP Server Side Validation.

This article will explain how you can create a HTML Form and do server side validation for the form. So the form which will display will be plain HTML and

Avoid things In Smarter PHP 7

mysql_ Functions : The mysql extension (and the mysql_* functions) have been deprecated since PHP 5.5. For a direct migration, you can use the mysqli extension and the mysqli_* functions

Using Type Declarations in PHP 7

PHP is considered to be a weak typed language. Using type declarations in PHP 7 simply means specifying which type of variable is being set instead of allowing PHP to