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

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -