+

PHP Ds / Vector insert () function

Syntax:
void public DsVector::insert ($index, $values)
Parameters : This function takes two parameters as above and described below:
  • $index:This parameter contains the index at which the element should be inserted.
  • $value:this parameter contains the value to be inserted.
Return value:this function is not returns no value.Exception : This function returns a OutOfRangeException if the index is invalid.The following programs illustrate the Ds / Vector::insert()in PHP:Program 1:  
// Declare a new vector $vector = new DsVector ([1, 2, 4, 5]);  
// Display the Vector element print_r ( $vector );  
// Use the insert() function to add
// element in the vector $vector -> insert (2, 3);  
// Show vector element print_r ( $vector );  
?>
Output:
DsVector Object ([0] = > 1 [1] = > 2 [2] = > 4 [3] = > 5) DsVector Object ([0] = > 1 [1] = > 2 [2] = > 3 [3] = > 4 [4] = > 5)
Program 2: < / p>  
// Declare a new vector $vector = new DsVector ([ "engineer" , "for" , "engineer" ]);  
// Display the Vector element print_r ( $vector );  
// Use the insert() function to add
// element in the vector $vector -> insert (1, "practice" );  
// Show vector element print_r ( $vector );  
?>
Output:
DsVector Object ([0] = > engineer [1] = > for [2] = > engineer) DsVector Object ([0] = > engineer [1] = > practice [2] = > for [3] = > engineer)
Link: http://php.net/manual/en/ds-vector.insert.php
Get Solution for free from DataCamp guru