How to append array element with increasing the array count with key in PHP? -


i've array $a

$a[1] = "a"; $a[2] = "b"; $a[3] = "c"; $a[4] = "d"; 

let's say, "x" new value want append middle position in array, want add 2nd array position $a[2] want increase count keys of array become like:

$a[1] = "a"; $a[2] = "x"; $a[3] = "b"; $a[4] = "c"; $a[5] = "d"; 

in case, want implement within loop checking conditions if, tried slice , splice both not working

do below:-

<?php  $a = array( 1=>'a', 2=>'b', 3=>'c', 4=>'d' );  $b = array( 'x' );   array_splice( $a, 1, 0, $b  );  $a = array_combine(range(1, count($a)), array_values($a));  print_r($a); 

output:-https://eval.in/837366


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -