PHP数组与字符串

发布时间:2009-02-11 12:39:00 阅读:1040次

<?php
$hobby=array("乒乓球","足球","篮球","排球");
echo implode(" ",$hobby)."<br/>";
?>

<?php
$str="中国|法国|德国|英国|意大利";
$country=explode("|",$str);
for($i=0;$i<=count($country)-1;$i++){
echo $country[$i]."<br/>";
}
?>

<?php
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
echo $ages["Peter"]."<br/>";
echo count($ages)."<br/>";
?>


<?php
$str="hello";
$s=str_split($str,2);
for($i=0;$i<=count($s);$i++){
echo $s[$i]."<br/>";
}
?>

<?php
$content=file_get_contents("test.txt");
$content=str_split($content);
for($i=0;$i<=count($content);$i++){
echo $content[$i]."<br/>";
}
$content=file("test.txt");
echo $content[0]."<br/>";

print_r($_SERVER);
?>

 

二维数组:

<?php
$families = array
(
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);

print_r($families["Brown"][0]);
?>


<?php
$society=array(
"family"=>array(
"father",
"mother",
"sister",
"myself"
)
)
?>
<?php
echo $society["family"][0];

$arrayFather = array(
array('a','b','c'),
array('d','e','f'),
array('g','h','i'));
for($i=0;$i<sizeof($arrayFather);$i++){
for($j=0;$j<sizeof($arrayFather[$i]);$j++){
echo $arrayFather[$i][$j]." ";
}
echo "<br>";
}
?>

二维数组与数据库:

<?php
$host = "localhost";    //主机名
$user = "root";         //mysql用户名
$password = "";     //mysql密码
$database = "test";   //mysql数据库名
$tables = "info";   //表名

$conn=mysql_connect("$host","$user","$password") or die("数据库打开出错");
mysql_select_db("$database");

    $query="select * from $tables";
 mysql_query("set names gb2312");
     $result=mysql_query($query,$conn);
     $i=0;
     $j=0;
       while($row=mysql_fetch_array($result)){
       $array[$i][$j] = $i.$j.$row["title"];
       $array2[$i][$j] = $row["id"];
      $j++;
      if($j==3) {
      $i++;
      $j=0;
          }
        }
      $amax=count($array);
     $rows=2;

   for ($x=0; $x<=$amax-1; $x++) {
        for ($y=0; $y<= $rows; $y++) {
          echo "<a href=".$array2[$x][$y].">".$array[$x][$y]."</a>|" ;
           }
           echo "<p>";
     }
?>

一维数组与数据库:
<?php
$cn=mysql_connect("localhost","root","");
$db=mysql_select_db("test");
$sql="select * from info";
mysql_query("set names gb2312");
$result=mysql_query($sql);
$i=0;
while($row=mysql_fetch_array($result)){
$title[$i]="<a href=detailinfo.php?id=".$row["id"].">".$row["title"]."</a>";
$i=$i+1;
}
?>

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:PHP数组与字符串 出自老鄢博客 | 欢迎分享