Home >headlines >Detailed explanation of php arrays

Detailed explanation of php arrays

无忌哥哥
无忌哥哥Original
2018-06-28 09:42:141898browse

* An array is an ordered collection of key names and values ​​

* 1. Category:

* [Key name]

* 1.1 Index array [default]: The key name is an integer [it will be automatically converted if not]

* 1.2 Associative array: the key name is a string [can be converted into object properties]

* [Is the key value an array?]

* 1.1 One-dimensional array: The key value is a non-array type, the most commonly used

* 1.1 Multi-dimensional array: The key value is still an array, the most commonly used is a two-dimensional array, nesting is not recommended Too deep

* 2. Create:

* 2.1 Unified creation: $arr = [element1,element2,...];

* 2.2 Create one by one: $arr =[];$arr[]=element1;$arr[]=element2,...

* 3. Access: square brackets plus key name [key]

* 3.1 Overall: print_r($arr); var_dump($arr);

* 3.3 One by one: echo $array[key]: $array['name'];

* 4. Update:

* 4.1 Overall: It needs to be implemented through loop traversal: foreach($arr as $value){//...};

* 4.2 One by one: $arr[key] = new_value;

* 4.3 Clear or rebuild: $arr=[]; $arr = [1,2,3...]; Re-declaring it with the original name will overwrite the original array

* 5. Delete :

* 5.1 Overall: unset($arr);

* 5.2 Single deleted key name without rearrangement: unset($arr[key]);

* 5.3 Single deletion key name rearrangement [for index array]: array_splice($arr,$start,$count,[$newEle]);

* 5.4 Delete null value elements: array_filter($arr);

* 5.5 Delete specific elements: foreach if unset

//1.Create

$city = ['合肥','上海','杭州','南京']; //索引数组
$user = ['id'=>10,'name'=>'Peter','course'=>'php','grade'=>99]; //关联数组

//2.Access

print_r($user);  //整体输出
echo &#39;<hr>&#39;;
echo $user[&#39;name&#39;];  //查看单个元素
echo &#39;<hr>&#39;;

//3.Update

$user[&#39;name&#39;]=&#39;朱老师&#39;; //更新操作
echo $user[&#39;name&#39;];  //再次查看

//4.Delete

unset($city);  //删除整个数组
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($city); //查看不存在的变量会报错,加@符可忽略Notice级错误
echo &#39;<hr>&#39;;
unset($user[&#39;course&#39;]);  //删除单个元素
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($user);  //整体输出, $user[&#39;course&#39;]元素已经不存在了
echo &#39;<hr>&#39;;
$city = [&#39;合肥&#39;,&#39;上海&#39;,&#39;杭州&#39;,&#39;南京&#39;]; //索引数组
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($city);  //原始索引数组
echo &#39;<hr>&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
//array_splice(arr,start,end,preserve):从数组特定位置取出指定数量的元素
//返回取出的数据,取出的数据从原始数组中删除掉
//从$city的第二个元素开始,取出2个并返回它们
print_r(array_splice($city,1,2)); 
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
//再次查看,会发现取出的元素,已经从原始数组中消失了
print_r($city);  //整体输出
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn