我正在尝试过滤结果数组,以排除字符串中具有某些单词的项目。我进行了搜索,我认为我已经接近了。这是我到目前为止的内容:
$ page是具有3个值的多维数组,我只关心[0]值,不需要其他值。
$popular = array();
$validresults = array('blog','ans-blog','podcast');
$i = 0;
foreach ($results as $page) {
$string = $page[0];
$url_string = end(explode('/', $string));
if (in_array($url_string,$validresults)){
$popular[$i] = $page[0];
$i++;
}
}
sort($popular);
print_r($popular);
最终的数组应该看起来像这样,只有所需的结果。
[0] => /ans-blog/
[1] => /ans-blog/2009/07/24/blah-blah-blah-blah/
[2] => /ans-blog/2010/08/05/blah-blah-blah-blah/
[3] => /ans-blog/2011/05/04/blah-blah-blah-blah/
[4] => /ans-blog/2011/11/15/blah-blah-blah-blah/
[5] => /ans-blog/2012/09/26/blah-blah-blah-blah/
[6] => /ans-blog/2013/10/24/blah-blah-blah-blah/
[7] => /ans-blog/2013/11/30/blah-blah-blah-blah/
[8] => /ans-blog/2015/07/03/blah-blah-blah-blah/
[9] => /ans-blog/2018/07/23/blah-blah-blah-blah/
[10] => /blog/2009/08/blah-blah-blah-blah/
[11] => /blog/2015/02/blah-blah-blah-blah/
[12] => /blog/2015/06/blah-blah-blah-blah/
[13] => /blog/2015/07/blah-blah-blah-blah/
[14] => /blog/2017/02/blah-blah-blah-blah/
[15] => /blog/2018/07/blah-blah-blah-blah/
[16] => /home/
[17] => /home/2018t2-courses/
[18] => /home/on-demand-courses/
[19] => /home/steps-registration/
[20] => /moodle/course/view.php?id=12
[21] => /moodle/course/view.php?id=45
[22] => /moodle/login/index.php
[23] => /moodle/my/
[24] => /podcast/
我希望最终数组仅包含URL中具有以下任何数组项的项:
array('blog','ans-blog','podcast');
我收到此错误:
PHP注意:在第91行的/var/www/vhosts/mydomain.org/httpdocs/mustread/HelloAnalytics.php中,只能通过引用传递变量
提前感谢您的帮助。
答案 0 :(得分:0)
foreach ($results as $page) {
$string = $page[0];
$url_string = explode('/', $string);
if (in_array($url_string[1],$validresults)) {
$popular[$i] = $page[0];
$i++;
}
}
添加了[1]->已解决。谢谢大家。
答案 1 :(得分:0)
请检查以下代码:
$popular = array();
$validresults = array('blog','ans-blog','podcast');
foreach ($results as $page) {
$uri_parts = explode('/', trim($page[0], '/'));
$url_string = end($uri_parts);
if (in_array($url_string, $validresults)) {
$popular[] = $page[0];
}
}
sort($popular);
print_r($popular);