php学习笔记之数组操作//待完善v10

1、array_map(callable, array)

1.1 将数组中value按照callable中原则进行逐一转换

代码:

$result = array_map(function($var){return strtolower($var);
}, [‘php’, ‘laravel’, ‘symfony’]);

结果:

array:3 [▼
0 => “php”1 => “laravel”2 => “symfony”
]

类比js中array.prototype.map():
代码:

[‘php’, ‘laravel’, ‘symfony’].map(function(str){return str.tolowercase()});

结果:

[“php”, “laravel”, “symfony”]

1.2 将几个数组按照每一个value合并为一个数组
代码:

$arr1 = [‘php’, ‘js’, ‘laravel’];
$arr2 = [‘array_map’, ‘map’, ‘array_map’];
$result1 = array_map(null, $arr1, $arr2);

结果:

array:3 [▼
0 => array:2 [▼
0 => “php”1 => “array_map”
]
1 => array:2 [▼
0 => “js”1 => “map”
]
2 => array:2 [▼
0 => “laravel”1 => “array_map”
]
]

2、array_unique(array)

可对数组value去重
代码:

$arr_unique1 = [‘php’, ‘laravel’, ‘js’, ‘js’, ‘laravel’];
$arr_unique2 = [4, ‘4’, 5, ‘5’, 6, 5, ‘6’, ‘7’];
array_unique($arr_unique1);
array_unique($arr_unique2);

结果:

array:3 [▼
0 => “php”1 => “laravel”2 => “js”
]
array:4 [▼
0 => 42 => 54 => 67 => “7”
]

3、array_flip(array)

将数组key和value相互转换
代码:

$arr_flip = [‘php’=>’php’,’laravel’=>’laravel’, ‘symfony’];
array_flip($arr_flip);

结果:

array:3 [▼
“php” => “php””laravel” => “laravel””symfony” => 0
]

可利用这个函数对数组的value去重:

$arr_flip = [‘php’=>’php’,’laravel’=>’laravel’, ‘symfony’, ‘php’];
array_flip(array_flip($arr_flip));

结果:

array:3 [▼
1 => “php””laravel” => “laravel”0 => “symfony”
]

‘).addclass(‘pre-numbering’).hide();
$(this).addclass(‘has-numbering’).parent().append($numbering);
for (i = 1; i

Posted in 未分类

发表评论