Adding items in PHP
If you're
referring to adding items to an array or a list in PHP.
Examples:- Adding Items to an Indexed Array:
<?php
// Creating an
indexed array
$fruits =
array("apple", "orange", "banana");
// Adding an item
at the end of the array
$fruits[] =
"grape";
// Adding multiple
items at the end of the array
$fruits[] =
"kiwi";
$fruits[] =
"pineapple";
// Displaying the
updated array
print_r($fruits);
?>
Adding Items to an Associative Array:
<?php
// Creating an
associative array
$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);
// Adding a new
key-value pair
$person["occupation"]
= "Engineer";
// Displaying the
updated associative array
print_r($person);
<?
Adding Items to the Beginning of an Array:
<?php
// Creating an
indexed array
$numbers =
array(3, 5, 7);
// Adding an item
at the beginning of the array
array_unshift($numbers,
1);
// Displaying the
updated array
print_r($numbers);
?>
Adding Items Using the `array_push()`
Function:
<?php
// Creating an
indexed array
$colors =
array("red", "blue", "green");
// Adding an item
at the end of the array using array_push
array_push($colors,
"yellow");
// Adding multiple
items at the end of the array using array_push
array_push($colors,
"orange", "purple");
// Displaying the
updated array
print_r($colors);
?>
Adding Items to a List Using `explode()`
and `array_merge()`:
<?php
// Existing
comma-separated list
$existingList =
"apple, orange, banana";
// Exploding the
existing list into an array
$items =
explode(", ", $existingList);
// Adding a new
item
$items[] =
"grape";
// Merging the
updated items into a new list
$newList =
implode(", ", $items);
// Displaying the
updated list
echo $newList;
?>
These examples
cover various ways to add items to arrays or lists in PHP.
Adding Items to an Associative Array Using
Square Bracket Notation:
<?php
// Creating an
associative array
$person = [
"name" => "Alice",
"age" => 25,
"city" => "London"
];
// Adding a new
key-value pair
$person["occupation"]
= "Software Developer";
// Displaying the
updated associative array
print_r($person);
?>
Adding Items to an Array with Specific
Keys:
<?php
// Creating an
indexed array
$fruits =
["apple", "orange", "banana"];
// Adding an item
with a specific key
$fruits[3] =
"kiwi";
// Adding multiple
items with specific keys
$fruits[4] =
"pineapple";
$fruits[5] =
"grape";
// Displaying the
updated array
print_r($fruits);
?>
Adding Items Using the `array_merge()`
Function:
<?php
// Creating an
indexed array
$firstArray = [1,
2, 3];
// Creating
another indexed array
$secondArray = [4,
5, 6];
// Merging the two
arrays
$mergedArray =
array_merge($firstArray, $secondArray);
// Displaying the
merged array
print_r($mergedArray);
?>
Adding Items to an Array with
`array_splice()`:
<?php
// Creating an
indexed array
$numbers = [1, 2,
3, 7, 8, 9];
// Adding items at
a specific position
array_splice($numbers,
3, 0, [4, 5, 6]);
// Displaying the
updated array
print_r($numbers);
?>
Adding Items to the End of an Array Using
the `array_push()` Function:
<?php
// Creating an
indexed array
$colors =
["red", "blue", "green"];
// Adding an item
at the end of the array using array_push
array_push($colors,
"yellow");
// Adding multiple
items at the end of the array using array_push
array_push($colors,
"orange", "purple");
// Displaying the
updated array
print_r($colors);
?>
These examples
showcase different methods for adding items to arrays or lists in PHP, whether
they are indexed arrays or associative arrays.
0 Comments