我有一个数组叫做$Bond
:
$Bond = array (
'Sean Connery' => 'Dr. No',
'George Lazenby' => 'On Her Majesty\'s Secret Service',
'Roger Moore' => 'Live and Let Die',
'Timothy Dalton' => 'The Living Daylights',
'Pierce Brosnan' => 'GoldenEye',
'Daniel Craig' => 'Casino Royal'
);
我只需要从“键”中提取姓氏并将其打印为大写即可。
我将如何处理?
您可以按照多种方式你可以用按键的数组array_keys()
,然后你的钥匙的阵列经历了for()
与分裂的名字explode()
,并转换为大写的姓氏(end($info)
)用strtoupper()
。参见代码:
<?php
$Bond = array (
'Sean Connery' => 'Dr. No',
'George Lazenby' => 'On Her Majesty\'s Secret Service',
'Roger Moore' => 'Live and Let Die',
'Timothy Dalton' => 'The Living Daylights',
'Pierce Brosnan' => 'GoldenEye',
'Daniel Craig' => 'Casino Royal'
);
$array_keys = array_keys($Bond);
for($i = 0; $i < count($array_keys); $i++) {
$info = explode(' ', $array_keys[$i]);
echo strtoupper(end($info)) . "<br>\n";
}
?>
或者,您可以使用以下foreach()
名称遍历数组,explode()
并end($info)
使用strtoupper()
类似以下内容将姓氏()转换为大写:
<?php
$Bond = array (
'Sean Connery' => 'Dr. No',
'George Lazenby' => 'On Her Majesty\'s Secret Service',
'Roger Moore' => 'Live and Let Die',
'Timothy Dalton' => 'The Living Daylights',
'Pierce Brosnan' => 'GoldenEye',
'Daniel Craig' => 'Casino Royal'
);
foreach($Bond as $key => $value) {
$info = explode(' ', $key);
echo strtoupper(end($info)) . "<br>\n";
}
?>
两种情况下的输出:
CONNERY
LAZENBY
MOORE
DALTON
BROSNAN
CRAIG
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句