Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
PHP - Change beginning of an Array

I'd like to change the beginning of my array in PHP. Currently I've got:

Array
(
  [0] => Bla##
  [1] => Bla##
  [2] => Bla##
  [3] => Bla##
  [4] => Bla##
  [5] => Bla##

but I want

Array
(
  [6] => Bla##
  [7] => Bla##
  [8] => Bla##
  [9] => Bla##
  [10] => Bla##
  [11] => Bla##

I used array_splice($array, 14, 0, 'Bla##'); to insert a value at a specific index of my array, but if I use this my array starts from 0 and not from 6.

Thanks in advance!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

$shift = 6;
$array = array_combine(range($shift, count($array)+$shift-1), $array);
over 4 years ago · Santiago Trujillo Report

0

Just shift all the array positions forward by 6 0->6, 1->7 etc

    $array = [Bla##,Bla##,Bla##,Bla##,Bla##,Bla##];
    $newarray = array();    // Shifted array
    for ($i=0; $i < count($array);$i++) {  
          $newarray[$i+6] = $array[$i];  
    } 
over 4 years ago · Santiago Trujillo Report

0

You can use array_walk():

$n = 6;
array_walk($arr, function($value) use (&$x, &$n) {$x[$n] = $value; $n++;});

PHP Demo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!