对于某些范围,我的数组会多次返回数据,对于第二个示例中通过foreach
过滤后的不同数组,该帖子仅包含1个唯一链接,因此其余链接根本不存在,应该打印只是来自image_1
的数据,这就是PHP代码。
$imagecounter = 1;
foreach ($html2->find('.post img') as $source) {
$link = $source->src;
if (strpos($link, 'https://www.example.com/wp-content/uploads/') !== false) {
$data['image_'.$imagecounter++.''] = $link;
}
}
这是数组。
Array
(
[url] => https://www.example.com/something-with-data/
[featured_image] => https://www.example.com/wp-content/uploads/2018/11/something-1.jpg
[name] => Main Categories
[image_1] => https://www.example.com/wp-content/uploads/2018/11/something-11.jpg
[image_2] => https://www.example.com/wp-content/uploads/2018/10/something-1.jpg
[image_3] => https://www.example.com/wp-content/uploads/2018/10/something-1.png
[image_4] => https://www.example.com/wp-content/uploads/2018/10/something-2.jpg
[image_5] => https://www.example.com/wp-content/uploads/2018/11/something-3.jpg
[image_6] => https://www.example.com/wp-content/uploads/2018/11/something-4.jpg
[image_7] => https://www.example.com/wp-content/uploads/2018/11/something-5.jpg
[image_8] => https://www.example.com/wp-content/uploads/2018/11/something-6.jpg
[image_9] => https://www.example.com/wp-content/uploads/2018/11/something-7.jpg
[image_10] => https://www.example.com/wp-content/uploads/2018/11/something-8.jpg
[image_11] => https://www.example.com/wp-content/uploads/2018/11/something-9.jpg
)
Array
(
[url] => https://www.example.com/a-completely-different-post
[featured_image] => https://www.example.com/wp-content/uploads/2018/10/UmekRLrlwK8.jpg
[name] => Main Categories
[image_1] => https://www.example.com/wp-content/uploads/2018/10/UmekRLrlwK8.jpg (THIS IS FROM THE NEW ITERATION)
[image_2] => https://www.example.com/wp-content/uploads/2018/10/something-1.jpg
[image_3] => https://www.example.com/wp-content/uploads/2018/10/something-1.png
[image_4] => https://www.example.com/wp-content/uploads/2018/10/something-2.jpg
[image_5] => https://www.example.com/wp-content/uploads/2018/10/something-3.jpg
[image_6] => https://www.example.com/wp-content/uploads/2018/10/something-4.jpg
[image_7] => https://www.example.com/wp-content/uploads/2018/10/something-5.jpg
[image_8] => https://www.example.com/wp-content/uploads/2018/10/something-6.jpg
[image_9] => https://www.example.com/wp-content/uploads/2018/10/something-7.jpg
[image_10] => https://www.example.com/wp-content/uploads/2018/10/something-8.jpg
[image_11] => https://www.example.com/wp-content/uploads/2018/10/something-9.jpg
[image_12] => https://www.example.com/wp-content/uploads/2018/10/something-10.jpg
)
答案 0 :(得分:0)
从一个空的$data
数组开始。
$data = [];
$imagecounter = 1;
foreach ($html2->find('.post img') as $source) {
$link = $source->src;
if (strpos($link, 'https://www.example.com/wp-content/uploads/') !== false) {
$data['image_'.$imagecounter++.''] = $link;
}
}
如果您需要存储多组$data
,则可以将每个新的$data
批次存储在父数组中。
foreach ($htmls as $html2) {
$data = [];
$imagecounter = 1;
foreach ($html2->find('.post img') as $source) {
$link = $source->src;
if (strpos($link, 'https://www.example.com/wp-content/uploads/') !== false) {
$data['image_'.$imagecounter++.''] = $link;
}
}
$result[] = $data;
}
答案 1 :(得分:-1)
如果我对,您想从阵列中过滤出重复的链接,则可以这样做。
您可以阅读有关array_unique
的信息$imagecounter = 1;
foreach ($html2->find('.post img') as $source) {
$link = $source->src;
if (strpos($link, 'https://www.example.com/wp-content/uploads/') !== false) {
$data['image_'.$imagecounter++.''] = $link;
}
}
$data = array_unique($data);
$imagecounter = 1;
foreach ($data as $link) { //this loop is here to reset the key value so it increments 1 by 1.
$data['image_'.$imagecounter++.''] = $link;
}