In PHP's daily operations, arrays are the most common structure, and we deal with array-related content almost every day. So the question is, how do you traverse and process arrays in general.
1、foreach
It's familiar, isn't it your favorite?
$arr = ['a', 'b', 'c'];
foreach ($arr as $key => $value) {
$arr[$key] = $value . '_i';
}
print_r($arr); // ['a_i', 'b_i', 'c_i'];
2、array_map
Use anonymous functions to handle each element in the array. A new array is returned when traversal is complete
$arr = ['a', 'b', 'c'];
$arr = array_map(function ($item) {
return $item . '_i';
}, $arr);
print_r($arr); // ['a_i', 'b_i', 'c_i'];
You can work with multiple arrays at the same time
$arr_1 = ['a', 'b', 'c'];
$arr_2 = ['you', 'good', 'is', 'original', 'sin'];
// $arr_n = [...];
$arr = array_map(function ($item_1, $item_2) {
return $item_1 . '_' . $item_2 . '_i';
}, $arr_1, $arr_2);
print_r($arr); ['a_ you _i', 'b_ is good_i', 'Are you c__i', '_original _i', '_sin_i']
Yes, the number of traversals is based on the longest array.
After traversing 3 times, $arr_1 is actually completed, so the values of $item_1 are null after that
3、array_walk
Traversing your array as a reference pass has no return value and can only process one array.
$arr = ['a', 'b', 'c'];
array_walk($arr, function (&$item) {
$item = $item . '_i';
});
print_r($arr); // ['a_i', 'b_i', 'c_i'];
Notice &$item, yes, there is an extra &, which means that your parameter is receiving data by reference. All you have to do is find a way to change this parameter.
If you are dealing with two-dimensional arrays, the processing method is also inseparable.
$arr = [
['name' => 'a'],
['name' => 'b'],
['name' => 'c']
];
array_walk($arr, function (&$item) {
$item['name'] = $item['name'] . '_i';
});
print_r($arr); // [['name' => 'a_i'], ['name' => 'b_i'], ['name' => 'c_i']];
4、for
This should be super generic syntax
$arr = ['a', 'b', 'c'];
for ($i = 0; $i < count($arr); ++$i) {
$arr[$i] = $arr[$i] . '_i';
}
print_r($arr); // ['a_i', 'b_i', 'c_i'];
Not recommended. It is not as efficient as foreach. And the writing method is slightly more cumbersome than forreach.
5、each
Haha, this function has been abandoned in PHP7.2 @deprecated and feels particularly old-fashioned. The execution efficiency of this writing method is also not good, and novices should give up directly.
$arr = ['a', 'b', 'c'];
while (list($key, $value) = each($arr)) {
$arr[$key] = $value . '_i';
}
print_r($arr); // ['a_i', 'b_i', 'c_i'];
6、reset
To be precise, it is not just a function to reset, but also a combination of multiple built-in functions, which is basically not used to write code
$arr = ['a', 'b', 'c'];
reset() pointer to the head, in fact, the default is to point to the beginning, you can ignore this sentence
reset($arr);
current() to get the element value of the current pointer
while ($value = current($arr)) {
key() to get the element subscript of the current pointer
$arr[key($arr)] = $value . '_i';
next() pointer to the next one
next($arr);
}
print_r($arr); // ['a_i', 'b_i', 'c_i'];
|