我有一个如下所示的数组......
int salesNum;
char lastName[100];
char firstName[50];
int salesLevel;
double salesAmount;
char oneline[200];
while(fgets(oneline, sizeof oneline, fPointer) != NULL) {
char sentinel;
if (5 != sscanf(oneline,"%d : %99[^:]: %49[^:]:%d :%lf %c",
&salesNum, lastName, firstName, &salesLevel, &salesAmount, &sentinel)) break;
printf("%d , '%s' , '%s' , %d , %lf\n",
salesNum, lastName, firstName, salesLevel, salesAmount);
}
我觉得这很难解释,但是我想把这个数组分解为仅显示没有重复的简化URL,所以它看起来像这样......
$urls = array(
"http://www.google.com",
"http://www.google.com/maps",
"http://www.google.com/mail",
"https://drive.google.com",
"https://www.youtube.com",
"https://www.youtube.com/feed/subscriptions",
"https://www.facebook.com/me",
"https://www.facebook.com/me/friends"
);
请注意,第二个数组中的最后一个URL仍然有它的路径。这是因为我还想要显示最低级别的路径
答案 0 :(得分:3)
基于@Tim的回答
foreach ($urls as &$url) {
$url_parts = parse_url($url);
$url = $url_parts["scheme"]."://".$url_parts["host"];
}
$urls = array_unique($urls);
答案 1 :(得分:1)
只需按相反的顺序对数组进行排序,然后创建一个由host:
索引的数组$urls = array(
"http://www.google.com",
"http://www.google.com/maps",
"http://www.google.com/mail",
"https://drive.google.com",
"https://www.youtube.com",
"https://www.youtube.com/feed/subscriptions",
"https://www.facebook.com/me",
"https://www.facebook.com/me/friends"
);
rsort($urls);
$return = [];
foreach($urls as $url) {
$host = parse_url($url, PHP_URL_HOST);
$return[$host] = $url;
}
$return = array_values($return); // To remove array keys, if desired.
反向排序的urls数组将是:
Array
(
[0] => https://www.youtube.com/feed/subscriptions
[1] => https://www.youtube.com
[2] => https://www.facebook.com/me/friends
[3] => https://www.facebook.com/me
[4] => https://drive.google.com
[5] => http://www.google.com/maps
[6] => http://www.google.com/mail
[7] => http://www.google.com
)
由于排序数组中的最后一个条目(每个主机名)是您想要的,并且它故意破坏任何现有的数组值,因此会输出:
Array
(
[www.youtube.com] => https://www.youtube.com
[www.facebook.com] => https://www.facebook.com/me
[drive.google.com] => https://drive.google.com
[www.google.com] => http://www.google.com
)
答案 2 :(得分:0)
试试这个:
$result = array();
array_push($result, $urls[0])
for($i=1; $i<count($urls); $i++)
{
$repeat = false;
foreach($result as $res)
{
if(strpos($urls[i], $res))
{
$repeat = true;
break;
}
}
if(!repeat)
array_push($result, $urls[i])
}
return $result;