php

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

In this tutorial, we are going to see how to add an element to the beginning of an array in PHP. You can use PHP’s array_unshift() function to append one or more values to the beginning of an array. Here is an example.
 

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

Output:

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

 

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 *