Showing posts with label PHP array_fill. Show all posts
Showing posts with label PHP array_fill. Show all posts

Monday, 6 July 2015

PHP: Add duplicate elements to an array

<?php
$columnsArr = array('','');
$colArr = array(2=>'DIABETES WITH ACUTE COMPLICATIONS'
,5=>'DIABETES WITH CHRONIC COMPLICATIONS'
,8=>'DIABETES'
,11=>'MALNUTRITION'
,14=>'OBESITY'
,17=>'RHEUMATOID'
,20=>'DRUG/ALCOHOL'
,23=>'PSYCH'
,26=>'NEURO'
,29=>'CARDIAC DISEASE'
,32=>'VASCULAR DISEASE'
,35=>'LUNG DISEASE'
,38=>'KIDNEY DISEASES');
foreach($colArr as $key=>$val){
  $columnsArr1 = array_fill($key,3, $val);
 $columnsArr = array_merge($columnsArr,$columnsArr1); 
}
echo "<pre>";
  print_r($columnsArr);
echo "</pre>";

?>

Wednesday, 24 September 2014

array_fill in PHP

PHP array_fill() function generates and then pass an array filled with the defined values.

Syntax:array_fill(start,number,value)

Parameters Description:
start : Required. A numeric value, defines the starting index of the key.
number : Required. A numeric value, defines the number of entries.
value : Required. defines the value to be added.

Example:

<?php
$days = array_fill( 2,3,"Sunday" );
print_r( $days );
?>

O/P:


Array (
           [2] => Sunday
           [3] => Sunday
           [4] => Sunday
         )