NotALeeecher
Anime University Lecturer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
500 XP
In my previous tutorial, I've created the Basic Array Functions. To follow up the tutorials about array, I've created another which will give you knowledge on how to add and delete item/s in array. This tutorial does not include a good design but will give you idea on the topic.
Creating our Form and Script
We create our add form, delete form and our scripts in adding and deleting items. To create the page, open your HTML code editor and paste the code below after the tag. We name this as "index.php".
Download
Creating our Form and Script
We create our add form, delete form and our scripts in adding and deleting items. To create the page, open your HTML code editor and paste the code below after the tag. We name this as "index.php".
- <!DOCTYPE html>
- <html>
- <head>
- <title>Adding and Deleting item/s in Array using PHP</title>
- </head>
- <body>
- <h3>Adding and Deleting item/s in Array using PHP</h3>
- <?php
- $my_array
=
array
(
'apple'
,
'orange'
,
'strawberry'
)
;
- ?>
- <label>Original Array:</label> <?php
print_r
(
$my_array
)
;
?>
<br><br>
- <form method="POST">
- <label>Add Item to Array:</label> <input type="text" name="add_array">
- <input type="submit" name="add" value="Add to Array">
- <form>
- <form method="POST">
- <label>Delete an Item from Array:</label> <input type="text" name="delete_array">
- <input type="submit" name="delete" value="Delete from Array">
- <form>
- <br><br>
- <?php
- if
(
isset
(
$_POST
[
'add'
]
)
)
{
- $add
=
$_POST
[
'add_array'
]
;
- array_push
(
$my_array
,
$add
)
;
- echo
"Added a new item: "
.
$add
.
"<br>"
;
- print_r
(
$my_array
)
;
- }
- if
(
isset
(
$_POST
[
'delete'
]
)
)
{
- $delete
=
$_POST
[
'delete_array'
]
;
- if
(
(
$key
=
array_search
(
$delete
,
$my_array
)
)
!==
false
)
{
- unset
(
$my_array
[
$key
]
)
;
- }
- echo
"Deleted an item: "
.
$delete
.
"<br>"
;
- print_r
(
$my_array
)
;
- }
- ?>
- </body>
- </html>
Download
You must upgrade your account or reply in the thread to view hidden text.