PHP/Data Structure/array
Содержание
- 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
<source lang="html4strict">
<? $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 ""$pets[0]".
\n"; print "$person[2].
\n"; print "{$customer["age"]}.
\n";?>
</source>
A list of numbers using an array variable
<source lang="html4strict">
<?php
$Inventory = array(1,2,3);
print "$Inventory[0]
\n";
print "$Inventory[1]
\n";
print "$Inventory[2]
\n";
An array variable!"; ?> </source>
Arrays
<source lang="html4strict">
<?
$myarray = array("Apples", "Oranges", "Pears"); $size = count($myarray); print_r($myarray);
?>
</source>
Converting an object to an array will convert properties to elements of the resulting array
<source lang="html4strict">
<?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); ?>
</source>
Create an array
<source lang="html4strict">
<? $fruits = array (
"fruits" => array("a"=>"orange", "b"=>"banana", "c"=>"apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third")
); ?>
</source>
Creates an array with keys 0 through 3
<source lang="html4strict">
<?php
$myarray = array("a", "b", "c", "d");
?>
</source>
Creating arrays with array()
<source lang="html4strict">
$vegetables = array("corn" => "yellow",
"beet" => "red", "carrot" => "orange");
$dinner = array(0 => "S",
1 => "L", 2 => "B");
$computers = array("t" => "A",
2600 => "B", "A" => "C");
?>
</source>
Creating multidimensional arrays with array()
<source lang="html4strict">
<? $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"));
?>
</source>
Creating numeric arrays with array()
<source lang="html4strict">
<? $dinner = array("A",
"B", "C");
print "I want $dinner[0] and $dinner[1]."; ?>
</source>
Demonstrate the Difference Between the Array "+" Operator and a True Array Union
<source lang="html4strict">
<?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 "\n";$union = array_unique(array_merge($first, $last));
echo ""; foreach ($union as $v) { echo "{$v} "; } echo "
\n";?>
</source>
Queue Handling Library
<source lang="html4strict">
<?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 "Queue size is: ", queue_size($myqueue), "
"; echo "Front of the queue is: ", queue_peek($myqueue), "
";queue_rotate($myqueue);
echo "Removed the element at the front of the queue: ", queue_dequeue($myqueue), "
";queue_destroy($myqueue); ?>
</source>
Setting up an associative array is similarly easy
<source lang="html4strict">
<? $myassocarray = array ("mykey" => "myvalue", "another" => "one"); while ($element = each ($myassocarray)) {
echo "Key - " . $element["key"] . " and Value - " . $element["value"] . "
";
} ?>
</source>
Stack Handling Library
<source lang="html4strict">
<?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 "Stack size is: ", stack_size($mystack), "
"; echo "Popped off the value: ", stack_pop($mystack), "
";stack_swap($mystack);
echo "Current top element is: ", stack_peek($mystack), "
";stack_destroy($mystack); ?>
</source>
Using the array() Function
<source lang="html4strict">
<?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;
?>
</source>
Using the array function to create an array of weekdays
<source lang="html4strict">
<?php $weekdays = array("Monday",
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
?>
</source>