PHP – Examples

Counting Array Elements or Counting Array Elements

<?php $dogs = array(‘A’ => ‘buzo’, ‘B’ => ‘tomy’, ‘C’ => ‘simba’); $birds = array(‘parrot’, ‘sparrow’, ‘kite’, ‘crow’); printf(“<p>There are %d dogs and %d birds.</p>”, count($dogs), count($birds)); $birds[] = ‘eagle’; printf (“<p>There are %d birds:</p>”, count($birds)); printf (“<pre>%s</pre>\n”, var_export($birds, TRUE)); $birds[7] = ‘heron’; unset ($birds[2]); printf (“<p>There are %d birds:</p>”, count($birds)); printf (“<pre>%s</pre>\n”, var_export($birds, TRUE)); ?>

array_map()

Applies the callback to the elements of the given arrays <?php function array_display($array, $pre=FALSE){    $tag = $pre ? ‘pre’ : ‘p’;    printf(“<%s>%s</%s>\n”, $tag, var_export($array, TRUE), $tag); } function calc_sqrt($num){    $i = $num < 0 ? ‘i’ :… (READ MORE)

Iterating an Array with foreach and while/list/each

<?php $pc_parts = array(); $pc_parts[“Memory”] = “256 MB”; $pc_parts[“CPU”] = “Athlon Thunderbird”; $pc_parts[“Video Card”] = “Matrox”; $pc_parts[“Hard Drive”] = “IBM”; foreach($pc_parts as $part_type => $part_name){    $buf = sprintf(“Part name is: <strong>%s</strong> Part value is: <strong>%s</strong>”, $part_type, $part_name);    echo $buf . “<br />”; } echo “<br />”; reset($pc_parts); while (list($part_type, $part_name) = each($pc_parts)){    $buf = sprintf(“Part name is: <strong>%s</strong> Part value is: <strong>%s</strong>”, $part_type, $part_name);    echo $buf . “<br />”; } ?>

array_merge

Merging two arrays <?php $alphabets = array(“B”,”D”,”L”,”P”); $numbers = array(“2″,”3″,”4″,”5″,”6”); $merge_array = array_merge($alphabets, $numbers); print_r($merge_array); ?> Array ( [0] => B [1] => D [2] => L [3] => P [4] => 2 [5] => 3 [6] => 4 [7]… (READ MORE)

Adding an Element to the End of an Array

<?php $languages = array(); $languages[] = ‘English’; $languages[] = ‘Dutch’; printf(“<p>Languages: %s.</p>\n”, implode(‘, ‘, $languages)); //Languages: English, Dutch. $languages = array(); array_push($languages, ‘German’, ‘French’); printf(“<p>Languages: %s.</p>\n”, implode(‘, ‘, $languages)); //Languages: German, French. ?>

Queue Handling Library

<?php function &queue_initialize(){    $new = array();    return $new; } function queue_destroy(&$queue){    unset($queue); } function queue_enqueue(&$queue, $value){    $queue[] = $value; } function queue_dequeue(&$queue){    return array_shift($queue); } function queue_peek(&$queue){    return $queue[0]; } function queue_size(&$queue){    return count($queue); } function queue_rotate(&$queue){… (READ MORE)

array()

Array is a set of elements. Creating an array in php <?php $vegetables = array( ‘spinach’ => ‘green’, ‘carrot’ => ‘orange’,  ‘lemon’  => ‘yellow’); $colors = array( 0 => ‘Red’, 1 => ‘Blue’, 2 => ‘Green’); $languages = array( ‘c’… (READ MORE)

Accessing Array Elements in PHP

<?php $my_array = array(); $pets = array(‘Cat’, ‘Dog’, ‘Horse’, ‘Hen’); $person = array(‘Ken’, ‘M’, 28, ‘MI’); $customer = array(‘first’ => ‘Josh’, ‘last’ => ‘D’, ‘age’ => 27, ‘state’ => ‘NY’); print “<p>’$pets[0]’.</p>\n”; // cat print “<p>$person[2].</p>\n”; // 28 print “<p>{$customer[‘age’]}.</p>\n”;… (READ MORE)