Remove ARRAY assets, and leave only required results

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Remove ARRAY assets, and leave only required results



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.





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 7 at 17:35


$url_string = end(explode('/', $string));





Yes why do you have end(explode when it looks like you have to use the first element of url string
– Seva Kalashnikov
Aug 7 at 17:36


end(explode





See this answer re: your PHP Notice - stackoverflow.com/questions/4636166/…
– WillardSolutions
Aug 7 at 18:30




2 Answers
2


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.



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered