码迷,mamicode.com
首页 > 其他好文 > 详细

array

时间:2015-05-30 14:47:02      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

array

array

来源: http://php.net/manual/zh/function.array.php

创建数组

$array = array(1, 1, 1, 1,  1, 8 => 1,  4 => 1, 19, 3 => 13);

$fruits = array (
    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "holes"   => array("first", 5 => "second", "third")
);

is_array isset count

<?php
$array = array(1, 1, 1, 1,  1, 8 => 1,  4 => 1, 19, 3 => 13);
if (is_array($array)) {
    echo ‘true‘, ‘<br />‘;
}
echo count($array), ‘<br />‘;   // 7
?>
<?php
$food = array(‘fruits‘ => array(‘orange‘, ‘banana‘, ‘apple‘),
              ‘veggie‘ => array(‘carrot‘, ‘collard‘, ‘pea‘));
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8

// normal count
echo count($food); // output 2
?>

array_merge

<?php
$a = array(1, 2, 3);
$b = array(4, 5, 6);

$c=  array_merge($a, $b);
var_dump ($c);
?>

输出

$foo = array(‘bar‘ => ‘baz‘);
echo "Hello {$foo[‘bar‘]}!",‘<br>‘; // Hello baz!

next prev end reset

<?php
$transport = array(‘foot‘, ‘bike‘, ‘car‘, ‘plane‘);
$mode = current($transport); // $mode = ‘foot‘;
$mode = next($transport);    // $mode = ‘bike‘;
$mode = next($transport);    // $mode = ‘car‘;
$mode = prev($transport);    // $mode = ‘bike‘;
$mode = end($transport);     // $mode = ‘plane‘;
?>

key current

$array = array(
    ‘fruit1‘ => ‘apple‘,
    ‘fruit2‘ => ‘orange‘,
    ‘fruit3‘ => ‘grape‘,
    ‘fruit4‘ => ‘apple‘,
    ‘fruit5‘ => ‘apple‘);

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == ‘apple‘) {
        echo key($array).‘<br />‘;
    }
    next($array);
}

foreach 遍历数组

$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // 最后取消掉引用
foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}


$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br>\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
?>
<?php
/* foreach example 1: value only */
$a = array(1, 2, 3, 17);

foreach ($a as $v) {
   echo "Current value of \$a: $v.\n";
}

/* foreach example 2: value (with its manual access notation printed for illustration) */
$a = array(1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */

foreach ($a as $v) {
    echo "\$a[$i] => $v.\n";
    $i++;
}

/* foreach example 3: key and value */
$a = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);

foreach ($a as $k => $v) {
    echo "\$a[$k] => $v.\n";
}
?>

multi array

<?php
/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

/* foreach example 5: dynamic arrays */

foreach (array(1, 2, 3, 4, 5) as $v) {
    echo "$v\n";
}
?>

需要 php >= 5.50

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b\n";
}
?>

reset

返回 foreach
返回数组第一个单元的值,如果数组为空则返回 FALSE
reset pointer, start again on step one

<?php
$array = array(‘step one‘, ‘step two‘, ‘step three‘, ‘step four‘);
// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"

// skip two steps
next($array);
next($array);
echo current($array) . "<br />\n"; // "step three"

// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\n"; // "step one"

?>

each

<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>

// # $bar
// Array
// (
//     [1] => bob
//     [value] => bob
//     [0] => 0
//     [key] => 0
// )
<?php
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo);
print_r($bar);
?>

// # $bar
// Array
// (
//     [1] => Bob
//     [value] => Bob
//     [0] => Robert
//     [key] => Robert
// )
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br>\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}

array

标签:

原文地址:http://www.cnblogs.com/sunznx/p/4540240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!