PHP is still more commonly used, so I studied PHP two-dimensional arrays. When dealing with php arrays, there is a particularly frequent requirement, such as the following two-dimensional array:
1
2
3
4
5
6
7
8
9
10
|
$arr
=
array
(
1=>
array
(
'id'
=> 5,
'name'
=>
'张三'
),
2=>
array
(
'id'
=> 6,
'name'
=>
'李四'
)
);
|
The purpose is to get the set whose key is name and get this result:
1
2
3
4
|
$arr2
=
array
(
0=>
'张三'
,
1=>
'李四'
);
|
Here are a few ways:
1: The simplest, foreach iterates over the array:
1
2
3
|
foreach
(
$arr
as
$key
=>
$value
) {
$arr2
[] =
$value
[
'name'
];
}
|
2: Code with a slightly smaller amount of code, using the php method of array_map:
1
|
$arr2
=
array_map
(
'array_shift'
,
$arr
);
|
It means to remove the value at the beginning of each value of the $arr array, and return the removed value of each value that is removed. Note that the key of the new array $arr2 is still the key of the original array $arr
2.1: On the basis of method 2, you can open your mind a little bit. If you need to get the beginning or end column of each item of the two-dimensional array, you can also do the following:
1
2
|
$arr2
=
array_map
(
'reset'
,
$arr
);
$arr2
=
array_map
(
'end'
,
$arr
);
|
Haha, it's very convenient
3: The array_reduce method can also be used, but the code is slightly more, but the imagination space of this method (for other array value operations) is still quite large:
1
|
$arr2
=
array_reduce
(
$arr
, create_function(
'$result, $v'
,
'$result[] = $v["name"];return $result;'
));
|
The array_reduce method uses the callback function to iteratively operate on the value of the array, and the create_function is used to call back an anonymous method. The parameter $result of the anonymous method is the value generated by the previous iteration, and $v is the current value. Internal implementation Both get the value of "name" of each item in the array $arr and push it to the new $result array;
4: Finally, this ultimate method is really cool, one method can be done, and it is very flexible:
1
|
$arr2
= array_column(
$arr
,
'name'
);
|
The second parameter is the key name of the column you want to get, isn't it very convenient, but this method has a limitation, that is, the php version must be >= 5.5.
PS: Several ways to traverse a two-dimensional array in php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?php
//使用for循环遍历
$arr2
=
array
(
array
(
"张三"
,
"20"
,
"男"
),
array
(
"李四"
,
"25"
,
"男"
),
array
(
"王五"
,
"19"
,
"女"
),
array
(
"赵六"
,
"25"
,
"女"
));
echo
"<table border=2 bordercolor=red><tr><td>姓名</td><td>年龄</td& gt;<td>性别</td></tr>"
;
for
(
$i
=0;
$i
<4;
$i
++){
echo
"<tr>"
;
for
(
$j
=0;
$j
<3;
$j
++){
echo
"<td>"
;
echo
$arr2
[
$i
][
$j
];
echo
"</td>"
;
}
echo
"</tr>"
;
echo
"<br>"
;
}
echo
"</table>"
;
?>
//使用foreach遍历
<?php
$arr
=
array
(
'one'
=>
array
(
'name'
=>
'张三'
,
'age'
=>
'23'
,
'sex'
=>
'男'
),
'two'
=>
array
(
'name'
=>
'李四'
,
'age'
=>
'43'
,
'sex'
=>
'女'
),
'three'
=>
array
(
'name'
=>
'王五'
,
'age'
=>
'32'
,
'sex'
=>
'男'
),
'four'
=>
array
(
'name'
=>
'赵六'
,
'age'
=>
'12'
,
'sex'
=>
'女'
));
foreach
(
$arr
as
$k
=>
$val
){
echo
$val
[
'name'
].
$val
[
'age'
].
$val
[
'sex'
].
"<br>"
;
}
echo
"<p>"
;
?>
<?php
$arr
=
array
(
'one'
=>
array
(
'name'
=>
'张三'
,
'age'
=>
'23'
,
'sex'
=>
'男'
),
'two'
=>
array
(
'name'
=>
'李四'
,
'age'
=>
'43'
,
'sex'
=>
'女'
),
'three'
=>
array
(
'name'
=>
'王五'
,
'age'
=>
'32'
,
'sex'
=>
'男'
),
'four'
=>
array
(
'name'
=>
'赵六'
,
'age'
=>
'12'
,
'sex'
=>
'女'
));
foreach
(
$arr
as
$key
=>
$value
){
foreach
(
$value
as
$key2
=>
$value2
){
echo
$value2
;
}
echo
"<br>"
;
}
?>
|