0% found this document useful (0 votes)
6 views4 pages

Array Programs

The document contains PHP code snippets demonstrating various array operations such as inserting an element using array_splice, merging two arrays with array_merge, pushing elements from one array to another with array_push, and deleting elements from both associative and indexed arrays using array_search and unset. Additionally, it shows how to insert an element at the beginning of an array using array_unshift. Each code snippet is accompanied by output statements to display the results of the operations.

Uploaded by

KiranKumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Array Programs

The document contains PHP code snippets demonstrating various array operations such as inserting an element using array_splice, merging two arrays with array_merge, pushing elements from one array to another with array_push, and deleting elements from both associative and indexed arrays using array_search and unset. Additionally, it shows how to insert an element at the beginning of an array using array_unshift. Each code snippet is accompanied by output statements to display the results of the operations.

Uploaded by

KiranKumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1). <?

php

//Original Array on which operations is to be perform

$original_array = array( '1', '2', '3', '4', '5' );

echo 'Original array : ';

foreach ($original_array as $x)

echo "$x ";

echo "\n";

//value of new item

$inserted_value = '11';

//value of position at which insertion is to be done

$position = 2;

//array_splice() function

array_splice( $original_array, $position, 0, $inserted_value );

echo "After inserting 11 in the array is : ";

foreach ($original_array as $x)

echo "$x ";

?>

Output

Original array : 1 2 3 4 5

After inserting 11 in the array is : 1 2 11 3 4 5

2) <?php

$arr1 = array("Geeks", "g4g");

$arr2 = array("GeeksforGeeks", "Computer science portal");

// Get the merged array in the first array itself.

$arr1 = array_merge($arr1, $arr2);


echo "arr1 Contents:";

// Use for each loop to print all the array elements.

foreach ($arr1 as $value) {

echo $value . "\n";

?>

3). <?php

$arr1 = array(1, 2);

$arr2 = array(3, 4);

// arr2 elements are being pushed in the arr1.

array_push($arr1 , ...$arr2);

echo "arr1 = ";

// Use for each loop to print all the array elements.

foreach ($arr1 as $value) {

echo $value . ' ';

?>

<?php

// Declaring an associative array

$ass_arr = ["a" => "Geeks", "b" => "For", "c" => "Geeks"];

// Finding and deleting the element with value "For"

$key = array_search("For", $ass_arr);

if ($key !== false) {

unset($ass_arr[$key]);

}
// Printing array after deleting the element

print_r($ass_arr);

// Declaring an indexed array

$ind_arr = ["Geeks", "For", "Geeks"];

// Finding and deleting the element with value "For"

$index = array_search("For", $ind_arr);

if ($index !== false) {

unset($ind_arr[$index]);

// Printing array after deleting the element

print_r($ind_arr);

?>

<?php

// Declare an array

$array = array(

"GeeksforGeeks",

"Computer",

"Science",

"Portal"

);

// Declare a variable containing element

$element = "Welcome";

// User array_unshift() function to


// insert element at beginning of array

array_unshift( $array, $element );

print_r($array);

?>

You might also like