如何在 PHP 中打印数组数组的所有第一个值?
How do I print all the first values of an array's array in PHP?
我正在使用 'PHP Simple HTML DOM Parser' 库。
我的代码基本上遍历我的不同站点并列出我需要的所有相关锚点,我的问题是:如何在每个 "selector" 的锚点中打印 href
的第一个值 $memb(n)
$team
.
数组中的变量
这是我的代码:
$memb1 = 'http://www.xyz1.org';
$memb2 = 'http://www.abc3.org';
$memb(n) = '...etc...etc'
$teams = array(
array("url" => $memb1, "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),
array("url" => $memb2, "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),
array("url" => $memb(n), "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),...etc...etc
当运行 foreach循环是这样的:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
foreach ($html->find($site["selector"]) as $a) {
$links[] = $a->href;
}
}
?>
<pre>
<?php print_r($links);?>
</pre>
我从所有 $memb
变量中得到了我想要的所有选定锚点,但我试图在每个 "selector" 的锚点中打印 href 的第一个值$team
数组中的 $memb(n)
个变量,但我找不到执行此操作的方法。
我已经尝试过 print_r(array_values($links)[0]);
但我只从 $memb1
得到第一个锚点并且它停在那里,它不会继续打印 $memb2
的第一个锚点等等.
如何从每个不同的 $memb 站点打印第一个锚点(索引 0)?
如果我没猜错,试试这个:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
foreach ($html->find($site["selector"]) as $a) {
$links[] = $a->href;
break;
}
}
或者,你可以稍微优化一下:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
$link = $html->find($site["selector"], 0);
if (!empty($link)) $links[] = $link->href;
}
对调用 find()
的结果使用数组索引
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
$anchors = $html->find($site["selector"]);
if (!empty($anchors)) {
$links[] = $anchors[0]->href;
}
}
我正在使用 'PHP Simple HTML DOM Parser' 库。
我的代码基本上遍历我的不同站点并列出我需要的所有相关锚点,我的问题是:如何在每个 "selector" 的锚点中打印 href
的第一个值 $memb(n)
$team
.
这是我的代码:
$memb1 = 'http://www.xyz1.org';
$memb2 = 'http://www.abc3.org';
$memb(n) = '...etc...etc'
$teams = array(
array("url" => $memb1, "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),
array("url" => $memb2, "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),
array("url" => $memb(n), "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),...etc...etc
当运行 foreach循环是这样的:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
foreach ($html->find($site["selector"]) as $a) {
$links[] = $a->href;
}
}
?>
<pre>
<?php print_r($links);?>
</pre>
我从所有 $memb
变量中得到了我想要的所有选定锚点,但我试图在每个 "selector" 的锚点中打印 href 的第一个值$team
数组中的 $memb(n)
个变量,但我找不到执行此操作的方法。
我已经尝试过 print_r(array_values($links)[0]);
但我只从 $memb1
得到第一个锚点并且它停在那里,它不会继续打印 $memb2
的第一个锚点等等.
如何从每个不同的 $memb 站点打印第一个锚点(索引 0)?
如果我没猜错,试试这个:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
foreach ($html->find($site["selector"]) as $a) {
$links[] = $a->href;
break;
}
}
或者,你可以稍微优化一下:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
$link = $html->find($site["selector"], 0);
if (!empty($link)) $links[] = $link->href;
}
对调用 find()
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
$anchors = $html->find($site["selector"]);
if (!empty($anchors)) {
$links[] = $anchors[0]->href;
}
}