我试图仅从$member1
和$member2
等url的basename
到$member(n)
创建一个数组。
我使用的基数组看起来像这样是有原因的,因为我将它用于另一个foreach
循环。
所以这个方案看起来是这样的:
$member1 = 'http://www.bla-bla.com/aFolder/anotherFolder';
$member2 = 'http://www.yada-yada.com/yetAnotherFolder/innerFolder';
$member3 = 'http://www.boo-boo.com/abcs/folder';
//this is the main array
$teams = array(
array("url" => $member1, "selector" => "a"),
array("url" => $member2, "selector" => "input"),
array("url" => $member3, "selector" => "h1"),...,...);
我想构建一个foreach,它将在末尾创建一个具有basename($member(n))
值的数组。
我希望我的结果看起来像这样:
Array
(
[0] => anotherFolder
[1] => innerFolder
[2] => folder
)
到目前为止,我已经尝试了不同的方法来提取特定的值,但没有成功,我找不到如何提取特定值的方法。我会感谢你的帮助。
$basenames = [];
foreach ($teams as $team) {
$basenames[] = basename($team['url']);
}
function getBaseName($array){
$return=array();
foreach($array as $arr)
{
$exp=explode("/",$arr['url']);
$return[]=array_pop($exp);
}
return $return;
}
$basenames[]=getBaseName($teams);
如果我理解得对,试试这个:
$resArray = array();
foreach($teams as $team){
$resArray[] = basename($team['url']);
}
使用array_map
和basename
函数的解决方案:
...
$result = array_map(function($v){ return basename($v['url']); }, $teams);
$array = array();
foreach($teams as $team){
$array[] = basename($team["url"]);
}
print_r($array);
将打印
Array
(
[0] => anotherFolder
[1] => innerFolder
[2] => folder
)