概述
要访问一个变量的内容,可以直接使用其名称。如果该变量是一个数组,可以使用变量名称和关键字或索引的组合来访问其内容。
像其他变量一样,使用运算符=可以改变数组元素的内容。数组单元可以通过 array[key] 语法来访问。

PHP中常用的数组操作方法笔记整理

数组的基本操作
php定义数组:

<"key"] = "values"; 
"htmlcode">
<"htmlcode">
<"foo" => "bar",
    "bar" => "foo",
  ];

  print_r($array);//Array ( [foo] => bar [bar] => foo ) 

"htmlcode">
<"htmlcode">
<"htmlcode">
<"htmlcode">
<"htmlcode">
<"htmlcode">
 <"htmlcode">
<"htmlcode">
<"I","Am","A","PHP","er");
  $newArray = array_combine($number,$array);
  print_r($newArray);//Array ( [1] => I [2] => Am [3] => A [4] => PHP [5] => er ) 
"htmlcode">
<"red", "green", "blue", "yellow");
  count ($color); //得到4
  array_splice($color,1,1); //删除第二个元素
  print_r(count ($color)); //3
  echo $color[2]; //yellow
  echo $color[1]; //blue
"htmlcode">
<"red", "green", "blue", "yellow","blue","green");
  $result = array_unique($color);
  print_r($result);//Array ( [0] => red [1] => green [2] => blue [3] => yellow ) 
"htmlcode">
<"red","blue","red","Black");
  print_r($array);
  echo "<br />";
  $array = array_flip($array);//
  print_r($array);//Array ( [red] => 2 [blue] => 1 [Black] => 3 ) 
"htmlcode">
<meta charset="utf-8">
<"red","blue","red","Black");
  $result=array_search("red",$array)//array_search(value,array,strict)
  if(($result === NULL)){
    echo "不存在数值red";
  }else{
    echo "存在数值 $result";//存在数值 0 
  }
?> 

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!