1

I am trying to filter an array of results to exclude items that have certain words within the string. I've searched and I think I've come fairly close. This is what I have so far:

$page is a multi-dimension array with 3 values, I am only concerned with the [0] value, the other values are not required.

    $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);

The final array should look something like this, with the only the required results.

[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/

I would like the end array to only have the items that have any of these array items in the URL:

array('blog','ans-blog','podcast');

I get this error:

PHP Notice: Only variables should be passed by reference in /var/www/vhosts/mydomain.org/httpdocs/mustread/HelloAnalytics.php on line 91

Thank you in advance for the assist.

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
Simon Hart
  • 11
  • 2
  • 1
    So, how does the result look like? I guess, the `$url_string = end(explode('/', $string));` does not what you want to, but lets see, what is the result of this code? – AlexandrX Aug 07 '18 at 17:35
  • Yes why do you have `end(explode` when it looks like you have to use the first element of url string – Seva Kalashnikov Aug 07 '18 at 17:36
  • See this answer re: your PHP Notice - https://stackoverflow.com/questions/4636166/only-variables-should-be-passed-by-reference – WillardSolutions Aug 07 '18 at 18:30

2 Answers2

0
foreach ($results as $page) {
    $string = $page[0];
    $url_string = explode('/', $string);
    if (in_array($url_string[1],$validresults)) {
        $popular[$i] = $page[0];
        $i++;
    } 
}

Added [1] -> solved. Thanks everyone.

Simon Hart
  • 11
  • 2
0

Please check with the following codes:

$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);
Tanvir Ahmed
  • 483
  • 3
  • 10