二维数组排序-搜索-增删改查-多维数组递归
1. 二维数组排序-搜索-增删改查
// 二维数组排序-搜索-增删改查
$arrs = [
['id' => 3, 'num' => 11, 'title' => 'css'],
['id' => 2, 'num' => 14, 'title' => 'html'],
['id' => 4, 'num' => 12, 'title' => 'javascript'],
['id' => 1, 'num' => 13, 'title' => 'hello world!'],
];
$new = ['id' => 5, 'num' => 15, 'title' => 'hello php!'];
// 1. 新增
array_push($arrs, $new);
/* Array (
[0] => Array ( [id] => 3 [num] => 11 [title] => css )
[1] => Array ( [id] => 2 [num] => 14 [title] => html )
[2] => Array ( [id] => 4 [num] => 12 [title] => javascript )
[3] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
[4] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
)
*/
echo print_r($arrs, true), '<br>';
// 2. 降序
array_multisort($arrs, SORT_DESC, array_column($arrs, 'id'));
/* Array (
[0] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
[1] => Array ( [id] => 4 [num] => 12 [title] => javascript )
[2] => Array ( [id] => 3 [num] => 11 [title] => css )
[3] => Array ( [id] => 2 [num] => 14 [title] => html )
[4] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
)
*/
echo print_r($arrs, true), '<br>';
// 3. 求和
// 逐行 id * num 求和
$sum = array_reduce($arrs, function ($prev, $next) {
return $prev + $next['id'] * $next['num'];
});
// 197
echo print_r($sum, true), '<br>';
// 4. 查找
// 查找 id = 2 记录
$arr = array_filter($arrs, function ($item) {
return $item['id'] === 2;
});
// Array ( [3] => Array ( [id] => 2 [num] => 14 [title] => html ) )
echo print_r($arr, true), '<br>';
// 5. 搜索
// 搜索 title 包含 hello 的记录
$arr = array_filter($arrs, function ($item) {
return false !== stripos($item['title'], 'hello');
});
/* Array (
[0] => Array ( [id] => 5 [num] => 15 [title] => hello php! )
[4] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
)
*/
echo print_r($arr, true), '<br>';
// 6. 删除
$ids = [3, 5];
// 删除指定 $ids 中的全部记录
$arrs = array_diff_assoc($arrs, array_filter($arrs, function($item) use ($ids) {
return in_array($item['id'], $ids);
}));
/* Array (
[1] => Array ( [id] => 4 [num] => 12 [title] => javascript )
[3] => Array ( [id] => 2 [num] => 14 [title] => html )
[4] => Array ( [id] => 1 [num] => 13 [title] => hello world! )
)
*/
echo print_r($arrs, true), '<br>';
// 7. 修改
$id = 1;
// 修改指定 $id 记录的 title 为 hello php!
$arrs = array_map(function($item) use ($id) {
if ($id === $item['id']) $item['title'] = 'hello php!';
return $item;
}, $arrs);
/* Array (
[1] => Array ( [id] => 4 [num] => 12 [title] => javascript )
[3] => Array ( [id] => 2 [num] => 14 [title] => html )
[4] => Array ( [id] => 1 [num] => 13 [title] => hello php! )
)
*/
echo print_r($arrs, true), '<br>';
2. 多维数组自定义递归函数
// 多维数组,键合并
function array_merge_multi(...$args) {
$array = array();
foreach ( $args as $arg ) {
if ( is_array( $arg ) ) {
foreach ( $arg as $k => $v ) {
if ( is_array( $v ) ) {
$array[$k] = isset( $array[$k] ) ? $array[$k] : array();
$array[$k] = array_merge_multi( $array[$k], $v );
} else {
$array[$k] = $v;
}
}
}
}
return $array;
}
// 多维数值,删除值
function array_remove_value( &$arr, $var ) {
foreach ( $arr as $key => $value ) {
if ( is_array( $value ) ) {
array_remove_value( $arr[$key], $var );
} else {
$value = trim( $value );
if ( $value == $var ) {
unset( $arr[$key] );
} else {
$arr[$key] = $value;
}
}
}
}
// 多维数组,判断值
function deep_in_array( $value, $array ) {
foreach( $array as $item ) {
if ( !is_array( $item ) ) {
if ( $item == $value ) {
return true;
} else {
continue;
}
}
if ( in_array( $value, $item ) ) {
return true;
} else if ( deep_in_array( $value, $item ) ) {
return true;
}
}
return false;
}
// 多维数组,搜索键
function array_search_key( $needle, $haystack ) {
global $nodes_found;
foreach( $haystack as $key1 => $value1 ) {
if ( $key1 === $needle ) {
$nodes_found[] = $value1;
}
if ( is_array( $value1 ) ) {
array_search_key( $needle, $value1 );
}
}
return $nodes_found;
}
// 多维数组,搜索值
function array_search_re( $needle, $haystack, $a = 0, $nodes_temp = array() ) {
global $nodes_found;
$a++;
foreach( $haystack as $key1 => $value1 ) {
$nodes_temp[$a] = $key1;
if ( is_array( $value1 ) ) {
array_search_re( $needle, $value1, $a, $nodes_temp );
} else if ( $value1 === $needle ) {
$nodes_found[] = $nodes_temp;
}
}
return $nodes_found;
}