PHP Array

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

PHP Array

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 numaric array is where the array keys are used as a numaric value, or there is not needed to assigned a array key for that kind of php array.

Creating style of Numeric or Non Associative PHP array:

There is 2 type of creation style of a numaric or Non Associative arrays and they are by giving unic index and assign value on it or assign a nulk of value at a time or single 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 particuler key for each value that if to declare the array you must have to assign a key to insert a velue.

Creating style of Associative PHP array:

There is also 2 type of creation style of an associative arrays and they are by bulk of value at a time or single 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 multi dimensional array in nothing but array inside array, to create a multidimensional array you can use a Associative array inside a Non Associative array or vice versa. And to get that array you can use preciously 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.