VVB
VVB

Reputation: 7641

Unable to parse matching results in sphinx/php

How to parse below array returned by sphinx in php

php code

if ( $result === false ) {
      echo "Query failed: " . $cl->GetLastError() . ".\n";
  }
  else {
      if ( $cl->GetLastWarning() ) {
          echo "WARNING: " . $cl->GetLastWarning() . "<br>";
      }

        if ($result['0'] > 0) {
            // print_r($result['0']['attrs']);   
            $words[] = $result['0']['matches'];
            foreach ($words as $val) {
                echo "=> " . $val['keyword'] . $val['ref'];
            }     
        } else {
                echo 'No results found';        
        }
  }

Array
(
    [0] => Array
        (
            [error] => 
            [warning] => 
            [status] => 0
            [fields] => Array
                (
                    [0] => keyword
                    [1] => ref
                )

            [attrs] => Array
                (
                    [keyword] => 7
                    [ref] => 7
                )

            [matches] => Array
                (
                    [25367434949034415] => Array
                        (
                            [weight] => 1643
                            [attrs] => Array
                                (
                                    [keyword] => hunting
                                    [ref] => activity
                                )

                        )

                )

            [total] => 1
            [total_found] => 1
            [time] => 0.000
            [words] => Array
                (
                    [hunt] => Array
                        (
                            [docs] => 1
                            [hits] => 1
                        )

                )

        )

)

I want to parse 'matches' array and it's subarray values like keyword, ref etc.

Upvotes: 0

Views: 52

Answers (1)

barryhunter
barryhunter

Reputation: 21091

Lets concentrate on this bit

    if ($result['0'] > 0) {
        $words[] = $result['0']['matches'];
        foreach ($words as $val) {
            echo "=> " . $val['keyword'] . $val['ref'];
        }
    }

Firstly, the result isnt an integer, so shouldnt really be compared as one (although guess it might work)

Personally would recommend checking the actual matches - and using empty, which works fine on arrays...

    if (!empty($result['0']['matches'])) {

Then you for some reason add the matches to an array (thats what $arry[] = does, its effectively 'push')

... but also no point assigned to a variable, as only use it once (in the foreach)

... Plus generally the document_id is the index of the matches array, so expose that.

        foreach ($result['0']['matches'] as $doc_id => $val) {

finally, now have the matches, want the attribute in the attors array. so something like

            $a = $val['attrs'];
            echo $doc_id." => ".$a['keyword'].", ".$a['ref']."<br>\n";

putting it all together...

    if (!empty($result['0']['matches'])) {
        foreach ($result['0']['matches'] as $doc_id => $val) {
            $a = $val['attrs'];
            echo $doc_id." => ".$a['keyword'].", ".$a['ref']."<br>\n";
        }
    }

Upvotes: 1

Related Questions