1

I have this array called $top, It consist of vehicle models like so -

$top = array('tacoma', 'corolla', 'camry');

I would like to know if any of these values exist in a for loop var called $title, im using this to get an rss feed. I have tried using array_search with no luck like so -

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $c++;
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('date')->item(0)->nodeValue,
        );
    array_push($feed, $item);

}


for($x=0;$x<$limit;$x++) {
    $title = $feed[$x]['title'];
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $item = $feed[$x]['item'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    $desc = preg_replace('/[^A-Za-z0-9\-]/', ' ', $description);
    $title = strtolower($title);

    $find= array_search($top, $title);

    if ($find) {
        // insert all matches in database
    } else {
        echo 'nothing found';
    }

}

Here is an example of what $title produces - toyota corolla ce low miles (chico) $4500. I would like to identify if corolla exist in the title using the array $top.

Here is what I get when I echo $title -

2012 chevy camaro 45th anniversary edition (chico/orland) $115002006 mitsubishi raider dura-cross pick-up for sale (yuba city, ca.) $59002016 dodge ram 2500 (corning) $430002016 dodge ram 2500 (corning) $4300020062007 toyota corolla ce low miles (chico) $4500 etc... So what I would like is to pull all titles that contain one of the strings in the $top array.

4
  • did you apllied this if-else inside for loop? I am unable to see. Also $top = array(tacoma, corolla, camry); is invalid, it need to be $top = array('tacoma', 'corolla', 'camry');. Also we don't know what is $feed? can you show it's values? Commented Sep 6, 2017 at 17:16
  • Sorry about that, I updated with more info..Yes the if-else is inside the for loop Commented Sep 6, 2017 at 17:20
  • Yes the if-else is inside the for loop ? where? add it properly. not as separate logic Commented Sep 6, 2017 at 17:31
  • did you try array_key_exists(). for more info w3schools.com/php/func_array_key_exists.asp Commented Sep 6, 2017 at 21:25

2 Answers 2

1

You need to change tour code like below (changes given in comment):-

<?php
$feed = [];//create an empty array variable
$top = array('tacoma', 'corolla', 'camry'); // predefine array
foreach ($rss->getElementsByTagName('item') as $node) {
    //$c++; not needed because no where used
    $feed[] = [ 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('date')->item(0)->nodeValue,
        ]; // assign value directly
}

foreach($feed  as $fee){ //use foreach it will take care of indexes
    $title = $fee['title'];
    $link = $fee['link'];
    $description = $fee['desc'];
    $item = $fee['item'];
    $date = date('l F d, Y', strtotime($fee['date']));
    $desc = preg_replace('/[^A-Za-z0-9\-]/', ' ', $description);
    $title = strtolower($title);

    $find= array_search($top, $title);

    if (count(array_intersect($top, explode(" ",$title)))>0) { 
        echo $title. " matched";
    } else {
        echo 'nothing found';
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I get "nothing found" looped about 20 times but no matches
@RyanD show me the different value of $title.echo it and show me what you get
Ok I added it to the question
@RyanD check my answer now . I have edited if-else condition part.Please have a look
0

You can use array_intersect and count:

if (count(array_intersect($top, $title)) > 0) {
    // do something
} else {
    echo 'nothing found';
}

Explanation: When the intersection of two arrays leaves one or more values, then it is present, otherwise it is not.


Note: Make sure that you are over-writing $title every time inside the for loop. If you use if outside the for, only last value holds!

1 Comment

Thanks, it only produces a value when nothing is found. Doesn't show there were any intersects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.