Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

PHP Index Array


What is the exact meaning of the index array?

The index array is the array in which values of the array being assigned to an index number. As the name(index array) indicates, the index array has importance of the index numbers .

If you have made an example of an array , then there will be no problem in understand index array. Index means 1, 2, 3, 4, 5 but in the array, the index number starts from 0.The array index number 0,1,2,3, 4, ..... etc .If you had tried the example of PHP array, then index array will be easy to understand.

Use the index number to fetch the value from the array, we will also use key=>value. The keys and value method is also used in the associative array. Each array item is stored as a key and value pair. The index array allowed the numeric value. The index and associative array based on the key signification. The associative array contains the user-defined key index. The user can define the key with any name in the associative array. The array index number starts from 0. This is totally preset.

Definition and usage of the index array

PHP index array is a special type of array, which can store multiple values at a time.

PHP index array is represented by the number which starts from 0. We can store number, string, an object in the PHP array. All PHP arrays elements are assigned to an index number by default.


All array elements assigned to an index number .

How to create index array ?

The array() function is uded to create a index array.The index array has numeric index. i.e( 0,1,2,3,4,5,6............n)


    
     array(); 

Examples of the Index Array

The following example will help you understand the index array concepts.


Example

<?php
$array = array("Bike ", "Car ", "Bus", "Scooty");
var_dump($array);
?>
Run

In the above example, we created an array and used vehicles as items.

The zero(0) index number has value "bike".

One index number has value "car".

Two index number has value "Bus".

Three index number has value "Scooty".

We used PHP var_dumb() function to find out the complete array structure.

In the below example, we will use the key and value.

Example

<?php
$array = array(
         "car",
         "bike",
    6 => "bus",
         "scooty",
);
var_dump($array);
?>
Run

In the above example, we create an array of vehicle items. In this array, we declared 6 key for the bus. If we run the script code . it gives the output. Value for 0 index number = "car"

Value for 1 index number = "bike "

Value for 6(as defined) index number = "bus" (Here, array ignored index number( 2,3,4,5,) becuase of defined 6 as key .

Value for 7 index number = "scooty" (Starts from the 6,7,8 ,9 etc )

As you can see the last value "scooty" was assigned the key 7. This is because the largest integer key before that was 6.

The index Array is like a linear array that is considered the index numbers values in one row.

Create an Array

Create an simple array. The array contains 5 items 1,2,3,4,5, After created array , use print_r() function to get array structure. It's really simple form of array.

<?php
 
 $arr = array(1, 2, 3, 4, 5);
print_r($arr);
?>


 
Output-
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

Using loop with Array

Now use foreach loop with an array. We will use the foreach loop and unset() function for delete every item, but leave the array itself intact. The unset() function used to remove keys from an array

<?php
 $arr = array(1, 2, 3, 4, 5);
 foreach ($arr as $i => $value) {
    unset($arr[$i]);
}
print_r($arr);
?>
Output-
Array ( )

Append Array Item

Append an item with key.The new key is 5, instead of 0. We have unset the array.Apped an new item. The of 6 will be at 5 which is insted of 0.

<?php
  $arr = array(1, 2, 3, 4, 5);
foreach ($arr as $i => $value) {
    unset($arr[$i]);
}
$arr[] = 6;
print_r($arr);
?>


Output-
Array ( [5] => 6 )

Re-index the array item

Now re-index the array from 5 which is insted of 0 . 0 for 6 and 1 for 7 That's it .

<?php
 $arr = array(1, 2, 3, 4, 5);
foreach ($arr as $i => $value) {
    unset($arr[$i]);
}
$arr[] = 6;
$arr = array_values($arr);
$arr[] = 7;
print_r($arr);
?>

Output-
    

    
Array ( [0] => 6 
        [1] => 7 
      )
      

Please Share

Recommended Posts:-

Previous Posts:-

Featured Items:-


Integrate PayPal payment gateway in PHP with MYSQL database | PHP scripts

$12



Paypal Payment form in PHP website with MYSQL database | International Payments

$12



Complete Blog CMS system in PHP with MYSQL database | PHP scripts

$13




0 Comment