Question: How to add element to array?
$arrayData = array(0=>'one',1=>'two',3=>'three'); print_r($arrayData); /* Array ( [0] => one [1] => two [3] => three ) */ $arrayData['4']='four'; print_r($arrayData); /** Array ( [0] => one [1] => two [3] => three [4] => four ) * */
Question: How to add multiple element in array?
$arrayData = array(0=>'one',1=>'two',3=>'three'); print_r($arrayData); /* Array ( [0] => one [1] => two [3] => three ) */ $arrayData['4']='four'; $arrayData['5']='five'; print_r($arrayData); /** Array ( [0] => one [1] => two [3] => three [4] => four [5] => five ) * */
Question: How to replace multiple array element with single array?
$arrayData = array(0=>'one',1=>'two',3=>'three',4=>'four',5=>'five',6=>'six'); print_r($arrayData); echo " "; /* * Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six ) */ array_splice($arrayData, 1,2,array('r'=>'replaced')); print_r($arrayData); /** Array ( [0] => one [1] => replaced [2] => four [3] => five [4] => six ) * */
Question: How to add element to an begining of array?
$arrayData = array(0=>'one',1=>'two',3=>'three'); print_r($arrayData); /* Array ( [0] => one [1] => two [3] => three ) */ array_unshift($arrayData, "four", "five"); print_r($arrayData); /** Array ( [0] => four [1] => five [2] => one [3] => two [4] => three ) * */
Question: How to add elements at the end of array?
$arrayData = array(0=>'one',1=>'two',3=>'three'); print_r($arrayData); /* Array ( [0] => one [1] => two [3] => three ) */ array_push($arrayData, "four", "five"); print_r($arrayData); /** Array ( [0] => four [1] => five [2] => one [3] => two [4] => three ) * */