phparraysjsonstrposstripos

Display first if a string contains 2 elements of my array - PHP,


I have a array containing a word list :

myArray = array(first, time, sorry, table,...);

and a JSON :

{
    "products": {
        "0": {
            "title": "myTitle",
            "url": "xxxxxx",
            "id": "329102"
        },
        "1": {
            "title": "myTitle",
            "url": "",
            "id": "439023",
        },...
     }
}

I do a loop, if the title contains one of the words of myArray, I display it.

    function strposArray($haystack, $needle, $offset=0) {
        if(!is_array($needle)) $needle = array($needle);
        foreach($needle as $query) {
            if(stripos($haystack, $query, $offset) !== false) return true;
        }
        return false;
    }



    foreach ( $parsed_json['products'] as $item ) {

        if (!empty($item['title'])) { $title = $item['title']; } else { $title = ''; }


        if ( strposArray($title, $myArray) ) {

            echo '<li>' .$title. '</li>';

        }

    }

I have no problem with this code, but I would like to improve the result.

If a title contains multiple elements of myArray, I would like it to appear at the top of the list.

First -> multiple elements

second -> one element

Thank you in advance


Solution

  • This function should do exactly what you want:

    function sortByWords(array $words, array $products){
        $results = [];
        foreach($products as $product){
            $elementCount = 0;
            foreach($words as $word){
                if(stripos($product['title'], $word) !== false){
                    $elementCount++;
                }
            }
    
            if($elementCount > 0){
                $results[] = ['elementCount' => $elementCount, 'product' => $product];
            }
        }
    
        usort($results, function($a, $b){
            return $a['elementCount'] < $b['elementCount'];
        });
    
        return $results;
    }
    

    Try to var_dump the result of the function. The resulting array looks something like this:

    C:\Users\Thomas\Projects\file.php:28:
    array (size=3)
      0 => 
        array (size=2)
          'elementCount' => int 3
          'product' => 
            array (size=1)
              'title' => string 'Apple Orange Peach' (length=18)
      1 => 
        array (size=2)
          'elementCount' => int 2
          'product' => 
            array (size=1)
              'title' => string 'Apple Orange' (length=12)
      2 => 
        array (size=2)
          'elementCount' => int 1
          'product' => 
            array (size=1)
              'title' => string 'Peach' (length=5)
    

    This is how you access the results.

    $results = sortByWords($words, $products);
    foreach($results as $result){
        $product = $result['product'];
    
        // now you can access your title, url and id from the $product array.
        echo $product['title'];
    
        // if you need the number of elements in the title, you can use $result['elementCount']
    }