Blogger Information
Blog 9
fans 0
comment 0
visits 7590
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组分类和遍历,以及数组函数
金牌马甲
Original
971 people have browsed it

数组

1.1数组的分类

1.1.1索引数组

代码展示:
  1. <?php
  2. //索引数组
  3. $customer = [
  4. 1=>'001',
  5. 2=>'张三',
  6. 3=>35,
  7. 4=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. ?>
运行结果:
  1. Array
  2. (
  3. [1] => 001
  4. [2] => 张三
  5. [3] => 35
  6. [4] => 32050219840925301X
  7. )

1.1.2关联数组

代码展示:
  1. <?php
  2. //关联数组
  3. $customer = [
  4. 'id'=>'001',
  5. 'name'=>'张三',
  6. 'age'=>35,
  7. 'ID card'=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. ?>
运行结果:
  1. Array
  2. (
  3. [id] => 001
  4. [name] => 张三
  5. [age] => 35
  6. [ID card] => 32050219840925301X
  7. )

1.2数组的遍历

1.2.1数组遍历方法1

代码展示:
  1. <?php
  2. //索引数组
  3. $customer = [
  4. 1=>'001',
  5. 2=>'张三',
  6. 3=>35,
  7. 4=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. echo '<hr>';
  11. printf('[%s]=>%s<br>', key($customer), current($customer));
  12. next($customer);
  13. printf('[%s]=>%s<br>', key($customer), current($customer));
  14. next($customer);
  15. printf('[%s]=>%s<br>', key($customer), current($customer));
  16. next($customer);
  17. printf('[%s]=>%s<br>', key($customer), current($customer));
  18. ?>
运行结果:
  1. Array
  2. (
  3. [1] => 001
  4. [2] => 张三
  5. [3] => 35
  6. [4] => 32050219840925301X
  7. )
  8. [1]=>001
  9. [2]=>张三
  10. [3]=>35
  11. [4]=>32050219840925301X

1.2.2数组遍历方法2

代码展示:
  1. <?php
  2. //索引数组
  3. $customer = [
  4. 1=>'001',
  5. 2=>'张三',
  6. 3=>35,
  7. 4=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. echo '<br>';
  11. while(true){
  12. printf('[%s]=>%s<br>', key($customer), current($customer));
  13. if(next($customer)) continue;
  14. else break;
  15. }
  16. ?>
运行结果:
  1. Array
  2. (
  3. [1] => 001
  4. [2] => 张三
  5. [3] => 35
  6. [4] => 32050219840925301X
  7. )
  8. [1]=>001
  9. [2]=>张三
  10. [3]=>35
  11. [4]=>32050219840925301X

1.2.3数组遍历方法3

代码展示:
  1. <?php
  2. //关联数组
  3. $customer = [
  4. 'id'=>'001',
  5. 'name'=>'张三',
  6. 'age'=>35,
  7. 'ID card'=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. echo '<br>';
  11. //echo count($customer);
  12. for($i = 0; $i < count($customer); $i++){
  13. printf('[%s]=>%s<br>', key($customer), current($customer));
  14. next($customer);
  15. }
运行结果:
  1. Array
  2. (
  3. [id] => 001
  4. [name] => 张三
  5. [age] => 35
  6. [ID card] => 32050219840925301X
  7. )
  8. [id]=>001
  9. [name]=>张三
  10. [age]=>35
  11. [ID card]=>32050219840925301X

1.2.4数组遍历方法4

代码展示:
  1. <?php
  2. //关联数组
  3. $customer = [
  4. 'id'=>'001',
  5. 'name'=>'张三',
  6. 'age'=>35,
  7. 'ID card'=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. echo '<br>';
  11. foreach($customer as $value) {
  12. echo $value, '<br>';
  13. }
  14. ?>
运行结果:
  1. Array
  2. (
  3. [id] => 001
  4. [name] => 张三
  5. [age] => 35
  6. [ID card] => 32050219840925301X
  7. )
  8. 001
  9. 张三
  10. 35
  11. 32050219840925301X

1.2.5数组遍历方法5

代码展示:
  1. <?php
  2. //关联数组
  3. $customer = [
  4. 'id'=>'001',
  5. 'name'=>'张三',
  6. 'age'=>35,
  7. 'ID card'=>'32050219840925301X'
  8. ];
  9. printf('<pre>%s</pre>', print_r($customer, true));
  10. echo '<br>';
  11. foreach($customer as $key) {
  12. printf('[%s]=>%s<br>', $key, $value);
  13. }
  14. ?>
运行结果:
  1. Array
  2. (
  3. [id] => 001
  4. [name] => 张三
  5. [age] => 35
  6. [ID card] => 32050219840925301X
  7. )
  8. [001]=>
  9. [张三]=>
  10. [35]=>
  11. [32050219840925301X]=>

1.3数组函数

1.3.1直接返回键名组成的数组

  1. array_keys()
代码展示:
  1. <?php
  2. $customer = [
  3. 'id'=>'001',
  4. 'name'=>'张三',
  5. 'age'=>35,
  6. 'ID card'=>'32050219840925301X'
  7. ];
  8. print_r(array_keys($customer));
  9. ?>
运行结果:
  1. Array ( [0] => id [1] => name [2] => age [3] => ID card )

1.3.2判断数组中是否存在指定的键名

  1. array_key_exists()
代码展示:
  1. <?php
  2. $customer = [
  3. 'id'=>'001',
  4. 'name'=>'张三',
  5. 'age'=>35,
  6. 'ID card'=>'32050219840925301X'
  7. ];
  8. echo array_key_exists('sex',$customer) ? '存在':'不存在';
  9. ?>
运行结果:
  1. 不存在

1.3.3返回数组中所有的值(不保留键名)

  1. array_values()
代码展示:
  1. <?php
  2. $customer = [
  3. 'id'=>'001',
  4. 'name'=>'张三',
  5. 'age'=>35,
  6. 'ID card'=>'32050219840925301X'
  7. ];
  8. printf('<pre>%s</pre>', print_r(array_values($customer), true));
  9. ?>
运行结果:
  1. Array
  2. (
  3. [0] => 001
  4. [1] => 张三
  5. [2] => 35
  6. [3] => 32050219840925301X
  7. )

1.3.4在数组中搜索某个键值,并返回对应的键名

  1. array_search()
代码展示:
  1. <?php
  2. $customer = [
  3. 'id'=>'001',
  4. 'name'=>'张三',
  5. 'age'=>35,
  6. 'ID card'=>'32050219840925301X'
  7. ];
  8. var_dump(array_search('35', $customer));
  9. ?>
运行结果:
  1. string(3) "age"

1.3.5移除数组中重复的值

  1. array_unique()
代码展示:
  1. <?php
  2. $customer = [
  3. '1'=>'001',
  4. '2'=>2,
  5. '3'=>2,
  6. '4'=>'123'
  7. ];
  8. print_r(array_unique($customer));
  9. ?>
运行结果:
  1. Array ( [1] => 001 [2] => 2 [4] => 123 )
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:数组函数众多, 分类记忆比较好
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post