PHP/Data Structure/array — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
- 1 Accessing Array Elements
- 2 A list of numbers using an array variable
- 3 Arrays
- 4 Converting an object to an array will convert properties to elements of the resulting array
- 5 Create an array
- 6 Creates an array with keys 0 through 3
- 7 Creating arrays with array()
- 8 Creating multidimensional arrays with array()
- 9 Creating numeric arrays with array()
- 10 Demonstrate the Difference Between the Array "+" Operator and a True Array Union
- 11 Queue Handling Library
- 12 Setting up an associative array is similarly easy
- 13 Stack Handling Library
- 14 Using the array() Function
- 15 Using the array function to create an array of weekdays
Accessing Array Elements
<?
$my_array = array();
$pets = array("A", "B", "C", "D");
$person = array("R", "W", 24, "CA");
$customer = array("first" => "R", "last" => "W", "age" => 24, "state" => "CA");
print "<p>"$pets[0]".</p>\n";
print "<p>$person[2].</p>\n";
print "<p>{$customer["age"]}.</p>\n";
?>
A list of numbers using an array variable
<?php
$Inventory = array(1,2,3);
print "$Inventory[0]<br>\n";
print "$Inventory[1]<br>\n";
print "$Inventory[2]<br>\n";
print "<p>An array variable!";
?>
Arrays
<?
$myarray = array("Apples", "Oranges", "Pears");
$size = count($myarray);
print_r($myarray);
?>
Converting an object to an array will convert properties to elements of the resulting array
<?php
class myclass {
public $name;
public $address;
private $age;
function SetAge($age) {
$this->age = $age;
}
}
$obj = new myclass;
$obj->name = "John";
$obj->address = "Main Street";
$obj->SetAge(47);
$arr = (array)$obj;
print_r($arr);
?>
Create an array
<?
$fruits = array (
"fruits" => array("a"=>"orange", "b"=>"banana", "c"=>"apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
?>
Creates an array with keys 0 through 3
<?php
$myarray = array("a", "b", "c", "d");
?>
Creating arrays with array()
$vegetables = array("corn" => "yellow",
"beet" => "red",
"carrot" => "orange");
$dinner = array(0 => "S",
1 => "L",
2 => "B");
$computers = array("t" => "A",
2600 => "B",
"A" => "C");
?>
Creating multidimensional arrays with array()
<?
$meals = array("breakfast" => array("A","C"),
"lunch" => array("B", "E"),
"snack" => array("C","F"));
$lunches = array( array("Chicken","Eggplant","Rice"),
array("Eggplant","Tofu"));
$flavors = array("Japanese" => array("hot" => "1",
"salty" => "2"),
"Chinese" => array("hot" => "3",
"pepper-salty" => "4"));
?>
Creating numeric arrays with array()
<?
$dinner = array("A",
"B",
"C");
print "I want $dinner[0] and $dinner[1].";
?>
Demonstrate the Difference Between the Array "+" Operator and a True Array Union
<?php
$first = array("e", "h", "r", "j", "b");
$last = array("w", "e", "c");
$plus_union = $last + $first;
echo "<p>";
foreach ($plus_union as $v) { echo "{$v} "; }
echo "</p>\n";
$union = array_unique(array_merge($first, $last));
echo "<p>";
foreach ($union as $v) { echo "{$v} "; }
echo "</p>\n";
?>
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) {
$queue[] = array_shift($queue);
}
$myqueue =& queue_initialize();
queue_enqueue($myqueue, "Opal");
queue_enqueue($myqueue, "Dolphin");
queue_enqueue($myqueue, "Pelican");
echo "<p>Queue size is: ", queue_size($myqueue), "</p>";
echo "<p>Front of the queue is: ", queue_peek($myqueue), "</p>";
queue_rotate($myqueue);
echo "<p>Removed the element at the front of the queue: ", queue_dequeue($myqueue), "</p>";
queue_destroy($myqueue);
?>
Setting up an associative array is similarly easy
<?
$myassocarray = array ("mykey" => "myvalue", "another" => "one");
while ($element = each ($myassocarray)) {
echo "Key - " . $element["key"] . " and Value - " . $element["value"] . "<br />";
}
?>
Stack Handling Library
<?php
function &stack_initialize() {
$new = array();
return $new;
}
function stack_destroy(&$stack) {
unset($stack);
}
function stack_push(&$stack, $value) {
$stack[] = $value;
}
function stack_pop(&$stack) {
return array_pop($stack);
}
function stack_peek(&$stack) {
return $stack[count($stack)-1];
}
function stack_size(&$stack) {
return count($stack);
}
function stack_swap(&$stack) {
$n = count($stack);
if ($n > 1) {
$second = $stack[$n-2];
$stack[$n-2] = $stack[$n-1];
$stack[$n-1] = $second;
}
}
function stack_dup(&$stack) {
$stack[] = $stack[count($stack)-1];
}
$mystack =& stack_initialize();
stack_push($mystack, 73);
stack_push($mystack, 74);
stack_push($mystack, 5);
stack_dup($mystack);
echo "<p>Stack size is: ", stack_size($mystack), "</p>";
echo "<p>Popped off the value: ", stack_pop($mystack), "</p>";
stack_swap($mystack);
echo "<p>Current top element is: ", stack_peek($mystack), "</p>";
stack_destroy($mystack);
?>
Using the array() Function
<?php
/* Arrays $foo and $bar are equivalent arrays */
$foo = array("a" => 1, "b" => 2, "c" => 3);
$bar["a"] = 1;
$bar["b"] = 2;
$bar["c"] = 3;
?>
Using the array function to create an array of weekdays
<?php
$weekdays = array("Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday");
?>