• 升级为终身会员只需57元,即可任意下载本站所有教程!
  • 每天签到,可以赚取金币!
  • 赞积分可以兑换金币、兑换实物礼品!

6.4、使用数组(输出数组、统计元素个数、遍历数组、转换数组、数组排序、数组指针)

  • 高清线路
  • 备用线路
  • 图文解说

6.4.1输出数组

<?php
$prices = array("a" =>100, "b"=>10, "c"=>1);
print_r($prices);
var_dump($prices);
?>

6.4.2 统计元素个数

<?php
$a[0] = 1;
$a[3] = 3;
$a[6] = 5;
echo count($a);
?>

<?php
$products = array(array( 'TIR', 'Tires', 100),
               array( 'OIL', 'oil', 10),
               array( 'SPK', 'Spark Plugs', 4));
 echo count($products, 1);
?>

6.4.3 遍历数组

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


<?php
$prices["a"] = 100;
$prices["b"] = 10;
$prices["c"] = 1;
while(list($product, $price) = each($prices))
    echo "$product => $price<br />";
?> 


<?php
$prices["a"] = 100;
$prices["b"] = 10;
$prices["c"] = 1;
while(list($product, $price) = each($prices))
    echo "$product => $price<br />";
reset($prices) ;
while(list($product, $price) = each($prices))
    echo "$product => $price<br />";
?> 

6.4.4数组与字符串的转换

<?php
$php  = "PHP is a popular general-purpose scripting language";
$php1 = explode(" ", $php);
var_dump($php1);
?>


<?php
$array = array('ASP', 'PHP', 'JSP');
$str = implode(",", $array);
echo $str; 
?>

6.4.5 数组排序

<?php
$products = array( "Tires", "Oil", "Spark Plugs" );
sort($products);
print_r($products);
?>

<?php
$prices = array( 100, 10, 4 );
sort($prices);
print_r($prices);
?>


<?php
$prices = array( "Tires"=>100, "Oil"=>10, "Spark Plugs"=>4 );
asort($prices);
print_r($prices);
?>


<?php
$prices = array( "Tires"=>100, "Oil"=>10, "Spark Plugs"=>4 );
ksort($prices);
print_r($prices);
?>


<?php
$products = array( array( "TIR", "Tires", 100 ), 
          array( "OIL", "Oil", 10 ),
          array( "SPK", "Spark Plugs", 4 ) );
function compare($x, $y){ 
    if ( $x[1] == $y[1] )
        return 0;
    else if ( $x[1] < $y[1] )
        return -1;
    else
        return 1;
}
usort($products, "compare");
print_r($products);
?>


<?php
$products = array( array( "TIR", "Tires", 100 ), 
          array( "OIL", "Oil", 10 ),
          array( "SPK", "Spark Plugs", 4 ) );
function compare($x, $y){ 
    if ( $x[2] == $y[2] )
        return 0;
    else if ( $x[2] < $y[2] )
        return -1;
    else
        return 1;
}
usort($products, "compare");
print_r($products);
?>


<?php
$products = array( array( "TIR", "Tires", 100 ), 
          array( "OIL", "Oil", 10 ),
          array( "SPK", "Spark Plugs", 4 ) );
function reverseCompare($x, $y){ 
    if ( $x[2] == $y[2] )
        return 0;
    else if ( $x[2] < $y[2] )
        return 1;
    else
        return -1;
}
usort($products, "compare");
print_r($products);
?>

6.4.6 数组指针

<?php
$array = array(1, 2, 3);
$value = end($array);
while ($value){ 
    echo "$value<br>";
    $value = prev($array);
}
?>
63.1K
关于我们 | 联系我们 | 使用协议 | 版权声明 | 网站地图 | 广告服务 | 友情链接| RSS订阅|
粤ICP备15104870号-5

机会是留给有准备的人的,舒服是留给死去的人的!
社会是在不断地变化发展的,要使自己的思想适应新的变化,那就得学习,不学习就淘汰。 —— 毛泽东
声明:本站部份原创外均收集于网络闲散资源以及网友上传提供,收费下载只为服务器资费所需和防止别人恶意下载并高价盗卖等!
收集宗旨是为了不让好资源浪费,作者辛苦录制的视频无人知悉岂不可惜!同时也帮助一些无法支付超贵培训费的学员自学!如无意中侵犯了您的权益请联系删除!Thank !
'); })();