博客列表 >数组分类和遍历,以及数组函数

数组分类和遍历,以及数组函数

金牌马甲
金牌马甲原创
2020年09月08日 18:41:02974浏览

数组

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 )
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议