是否可以从数组值中获取/选择前两个字符? 例如:
Array
(
[0] => 1_name
[1] => 0_sex
[2] => 2_age
}
我需要从数组元素中的每个值中获取前两个字符。因此,如果我echo
,我可以得到这个结果。
First Element is 1_
Second Element is 0_
Third Element is 2_
任何人都知道如何解决这个问题?感谢
答案 0 :(得分:2)
您可以使用Sub findhours()
Dim hours As Range
Set hours = Application.Workbooks("vbstest.dec17").Worksheets("sheet1").Cells.Find("hours")
Dim thours As Range
Set thours = Application.Workbooks("vbstest.dec17").Worksheets("sheet2").Cells.Find("hours")
Dim z As Integer
Dim zh As Integer
z = hours.Column
zh = thours.Column
Dim x As Integer
Dim xh As Integer
x = hours.Row
xh = thours.Row
Application.Workbooks("vbstest.dec17").Worksheets("sheet1").Cells(x, z).Offset(0, 1).Value = 5
获取新数组,只包含每个项目的前两个字符:
Range(zh, xh + 1)
答案 1 :(得分:1)
以这种方式使用foreach
循环:
<?php
$a = ["1_name",
"0_sex",
"2_age"];
foreach ($a as $aa) {
echo substr($aa, 0, 2) . "\n";
}
输出:
1_
0_
2_
您也可以使用array_map()
函数返回数组。
array_map(function ($a) {
return substr($a, 0, 2);
}, $input);
答案 2 :(得分:0)
使用索引也适合你。
index.php
<?php
$array = [
'1_name',
'0_sex',
'2_age'
];
echo 'First element is ' . $array[0][0] . $array[0][1] . '<br>';
echo 'Second element is ' . $array[1][0] . $array[1][1] . '<br>';
echo 'Third element is ' . $array[2][0] . $array[2][1] . '<br>';
First element is 1_
Second element is 0_
Third element is 2_