Setting an Array's Size : Array Index « Data Structure « PHP






Setting an Array's Size

 
<?php
  $dogs = array('A' => 'AA', 'Bud' => 'BB','C' => 'D');
  $birds = array('parrot', 'magpie', 'lorakeet', 'cuckoo');

  $pups = array_pad($dogs, 6, 'mutt');
  $more_birds = array_pad($birds, 6, 'some bird');

  printf("<p>Pups:</p><pre>%s</pre>\n", var_export($pups, TRUE));
  printf("<p>More birds:</p><pre>%s</pre>\n", var_export($more_birds, TRUE));

  $pups = array_pad($dogs, -6, 'mutt');
  $more_birds = array_pad($birds, -6, 'some bird');

  printf("<p>Pups:</p><pre>%s</pre>\n", var_export($pups, TRUE));
  printf("<p>More birds:</p><pre>%s</pre>\n", var_export($more_birds, TRUE));

  printf("<p>Dogs:</p><pre>%s</pre>\n", var_export($dogs, TRUE));
  printf("<p>Birds:</p><pre>%s</pre>\n", var_export($birds, TRUE));

?>
  
  








Related examples in the same category

1.Adding elements with []
2.Alternating table row colors
3.Getting array size
4.Loops through an indexed array of fruit names and creates a new array with the count of each fruit name.