Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_unshift()

Insert values at the start of the array in their argument order and return the new element count.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

array_unshift(array &$array, mixed ...$values): int

Insert values at the start of the array in their argument order and return the new element count. Integer keys are renumbered, while string keys remain associated with their values. The input variable changes, so store the count separately when the resulting queue is needed.

Signature

array_unshift(array &$array, mixed ...$values): int

Example output

[3,["A","B","C"]]

Examples

<?php
$queue = [9=>'C'];
$size = array_unshift($queue, 'A','B');
echo json_encode([$size, $queue]);

Result

[3,["A","B","C"]]

Sources