博主信息
葡萄枝子
博文
50
粉丝
1
评论
0
访问量
5118
积分:0
P豆:100

二维数组排序-搜索-增删改查-多维数组递归

2021年01月31日 21:00:17阅读数:612博客 / 葡萄枝子/ PHP

二维数组排序-搜索-增删改查-多维数组递归

1. 二维数组排序-搜索-增删改查

  1. // 二维数组排序-搜索-增删改查
  2. $arrs = [
  3. ['id' => 3, 'num' => 11, 'title' => 'css'],
  4. ['id' => 2, 'num' => 14, 'title' => 'html'],
  5. ['id' => 4, 'num' => 12, 'title' => 'javascript'],
  6. ['id' => 1, 'num' => 13, 'title' => 'hello world!'],
  7. ];
  8. $new = ['id' => 5, 'num' => 15, 'title' => 'hello php!'];
  9. // 1. 新增
  10. array_push($arrs, $new);
  11. /* Array (
  12. [0] => Array ( [id] => 3 [num] => 11 [title] => css )
  13. [1] => Array ( [id] => 2 [num] => 14 [title] => html )
  14. [2] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  15. [3] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  16. [4] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
  17. )
  18. */
  19. echo print_r($arrs, true), '<br>';
  20. // 2. 降序
  21. array_multisort($arrs, SORT_DESC, array_column($arrs, 'id'));
  22. /* Array (
  23. [0] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
  24. [1] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  25. [2] => Array ( [id] => 3 [num] => 11 [title] => css )
  26. [3] => Array ( [id] => 2 [num] => 14 [title] => html )
  27. [4] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  28. )
  29. */
  30. echo print_r($arrs, true), '<br>';
  31. // 3. 求和
  32. // 逐行 id * num 求和
  33. $sum = array_reduce($arrs, function ($prev, $next) {
  34. return $prev + $next['id'] * $next['num'];
  35. });
  36. // 197
  37. echo print_r($sum, true), '<br>';
  38. // 4. 查找
  39. // 查找 id = 2 记录
  40. $arr = array_filter($arrs, function ($item) {
  41. return $item['id'] === 2;
  42. });
  43. // Array ( [3] => Array ( [id] => 2 [num] => 14 [title] => html ) )
  44. echo print_r($arr, true), '<br>';
  45. // 5. 搜索
  46. // 搜索 title 包含 hello 的记录
  47. $arr = array_filter($arrs, function ($item) {
  48. return false !== stripos($item['title'], 'hello');
  49. });
  50. /* Array (
  51. [0] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
  52. [4] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  53. )
  54. */
  55. echo print_r($arr, true), '<br>';
  56. // 6. 删除
  57. $ids = [3, 5];
  58. // 删除指定 $ids 中的全部记录
  59. $arrs = array_diff_assoc($arrs, array_filter($arrs, function($item) use ($ids) {
  60. return in_array($item['id'], $ids);
  61. }));
  62. /* Array (
  63. [1] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  64. [3] => Array ( [id] => 2 [num] => 14 [title] => html )
  65. [4] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
  66. )
  67. */
  68. echo print_r($arrs, true), '<br>';
  69. // 7. 修改
  70. $id = 1;
  71. // 修改指定 $id 记录的 title 为 hello php!
  72. $arrs = array_map(function($item) use ($id) {
  73. if ($id === $item['id']) $item['title'] = 'hello php!';
  74. return $item;
  75. }, $arrs);
  76. /* Array (
  77. [1] => Array ( [id] => 4 [num] => 12 [title] => javascript )
  78. [3] => Array ( [id] => 2 [num] => 14 [title] => html )
  79. [4] => Array ( [id] => 1 [num] => 13 [title] => hello php! )
  80. )
  81. */
  82. echo print_r($arrs, true), '<br>';

2. 多维数组自定义递归函数

  1. // 多维数组,键合并
  2. function array_merge_multi(...$args) {
  3. $array = array();
  4. foreach ( $args as $arg ) {
  5. if ( is_array( $arg ) ) {
  6. foreach ( $arg as $k => $v ) {
  7. if ( is_array( $v ) ) {
  8. $array[$k] = isset( $array[$k] ) ? $array[$k] : array();
  9. $array[$k] = array_merge_multi( $array[$k], $v );
  10. } else {
  11. $array[$k] = $v;
  12. }
  13. }
  14. }
  15. }
  16. return $array;
  17. }
  18. // 多维数值,删除值
  19. function array_remove_value( &$arr, $var ) {
  20. foreach ( $arr as $key => $value ) {
  21. if ( is_array( $value ) ) {
  22. array_remove_value( $arr[$key], $var );
  23. } else {
  24. $value = trim( $value );
  25. if ( $value == $var ) {
  26. unset( $arr[$key] );
  27. } else {
  28. $arr[$key] = $value;
  29. }
  30. }
  31. }
  32. }
  33. // 多维数组,判断值
  34. function deep_in_array( $value, $array ) {
  35. foreach( $array as $item ) {
  36. if ( !is_array( $item ) ) {
  37. if ( $item == $value ) {
  38. return true;
  39. } else {
  40. continue;
  41. }
  42. }
  43. if ( in_array( $value, $item ) ) {
  44. return true;
  45. } else if ( deep_in_array( $value, $item ) ) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. // 多维数组,搜索键
  52. function array_search_key( $needle, $haystack ) {
  53. global $nodes_found;
  54. foreach( $haystack as $key1 => $value1 ) {
  55. if ( $key1 === $needle ) {
  56. $nodes_found[] = $value1;
  57. }
  58. if ( is_array( $value1 ) ) {
  59. array_search_key( $needle, $value1 );
  60. }
  61. }
  62. return $nodes_found;
  63. }
  64. // 多维数组,搜索值
  65. function array_search_re( $needle, $haystack, $a = 0, $nodes_temp = array() ) {
  66. global $nodes_found;
  67. $a++;
  68. foreach( $haystack as $key1 => $value1 ) {
  69. $nodes_temp[$a] = $key1;
  70. if ( is_array( $value1 ) ) {
  71. array_search_re( $needle, $value1, $a, $nodes_temp );
  72. } else if ( $value1 === $needle ) {
  73. $nodes_found[] = $nodes_temp;
  74. }
  75. }
  76. return $nodes_found;
  77. }

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论