php

How to add an element to the end of an array in PHP

In this tutorial, we are going to see how to add an element to the end of an array in PHP. You can use PHP’s array_push() function to append one or more values to the end of an array. Check out the following example.
 

How to add an element to the end of an array in PHP
<?php
	$langages = array("PHP", "Java", "Python", "HTML", "CSS", "JavaScript");
	 
	// Add the element at the end of the array 
	array_push($langages, "Scala", "Perl");
	print_r($langages);
?>

Output:

Array ( 
	[0] => PHP 
	[1] => Java 
	[2] => Python 
	[3] => HTML 
	[4] => CSS 
	[5] => JavaScript 
	[6] => Scala 
	[7] => Perl 
)

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *