PHP Array

If you have a lot of data and needs to store into variables then its become very difficult to handle the variables, in this case, PHP array helped to store a group of data in an organized way, and PHP array helped to easily find the data stored on it,

PHP Array

There are 3 different types of PHP arrays:

1. Numeric arrays or Non-Associative arrays
2. Associative arrays
3. Multidimensional arrays

Numeric arrays or Non-Associative arrays

A numeric array is where the array keys are used as a numeric value, or there is not needed to assign an array key for that kind of PHP array.

Creating a style of Numeric or Non-Associative PHP array:

There is 2 type of creative style of a numeric or Non Associative array and they are by giving unique index and assign a value on it or assign a bulk of value at a time or single.

Example:

<?php
$example_array[0] = ‘Green’;
$example_array[1] = ‘Blue’;
$example_array[2] = ‘Red’;
?>

or

<?php
$example_array = array(‘Green’, ‘Blue’, ‘Red’);
?>

or

<?php
$example_array[] = ‘Green’;
$example_array[] = ‘Blue’;
$example_array[] = ‘Red’;
?>

Get Value from Numeric or Non Associative PHP array:

to get a value from numaric or non associative php array is very easy, just write array variable name and after that inside third bresses the index number, you can use foreach loop also to get the values from an array.

Example:

<?php
echo $example_array[1]; //it will print Blue
?>

Associative arrays

An associative arrays have the particular key for each value that is to declare the array you must have to assign a key to insert a value.

Creating a style of Associative PHP array:

There is also 2 type of creative style of associative arrays and they are by the bulk of value at a time or single.

Example:

<?php
$example_array[‘grn_ky’] = ‘Green’;
$example_array[‘ble_ky’] = ‘Blue’;
$example_array[‘rd_ky’] = ‘Red’;
?>

or

<?php
$example_array = array(‘grn_ky’ => ‘Green’, ‘ble_ky’ => ‘Blue’, ‘rd_ky’ => ‘Red’);
?>

Get Value from Associative PHP array:

To get a value from associative php array just write array variable name and after that inside third bresses the index name inside quote or use a veriable where the name is previously assigned, you can also use foreach loop to get the values from an array.

Example:

<?php
echo $example_array[‘ble_ky’]; //it will print Blue
?>

Multidimensional arrays

A multidimensional array is nothing but array inside the array, to create a multidimensional array you can use an Associative array inside a Non Associative array or vice versa. And to get that array you can use the previously written method, or a loop also.

An Example of PHP multidimensional array is given below,

Example:

<?php
$example_array = array(
‘grn_ky’ => ‘Green’,
‘loved_cars’ => array(
‘normal’ => array(‘Honda’, ‘Maruti’, ‘BMW’),
‘luxcery’ => array(‘Ferrari’, ‘Lamborghini’, ‘BMW’),
‘hibrid’ => array(‘Lamborghini’, ‘Ferrari’),
),
‘rd_ky’ => ‘Red’
);

print_r($example_array);
?>

I home you have learned PHP Array and the post helped you to learned php,

Thanks for Reading

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.