Sphinx Get Word in Result through PHP API - php

The Sphinx PHP API returns a "matches" array after executing "Query();". It also returns a "Words" array. What I'm wondering is if there is a way that for each match in the "matches" array if it would show what word was used to give that result it's weight.
For example, my query is...
"Green Banana | Yellow Banana"
The results look something like.
[18403206462384766539] => Array
(
[weight] => 4553
[attrs] => Array
(
)
)
What I'd love to see is something more like.
[18403206462384766539] => Array
(
[weight] => 4553
[attrs] => Array
(
)
[word] => "Green Banana"
)
I want to avoid having to make two separate queries and then having to compare the weights across both result arrays to find the highest one because I plan on having a query with many hundreds or thousands of OR'd phrases.

Not really. There is however PACKEDFACTORS() - which might give you some useful information.
Its a function so can be used in the `SetSelect call.

Related

List all combinations of option strings while following order of supplied array and rules of specific options - php

I am working on a project where a company sells a multitude of valves with a vast array of variations and need to build a site map with links to every valve data as the current navigation does not use conventional links but codes built via a user interacting with the many options on the page. I have been able to generate everything up until the options which begin to get very tricky because of the vast array of combinations of these options there are and the rules that they follow.
I am attempting to build every option string combinations with the rules you can find below.
Given the array of option codes (at the end of the post) I must generate a list of all possible combinations but they must follow the order that they are presented and follow the rules given by the row_mod. For example when the option_code of O is in the combinations the option_code of ON and OP are not allowed to be in the combination.
The only row_mod info that needs to be followed currently when building these strings is the "naWithOp##" as that refers to another option that is not allowed to be with it.
Some of the possible combinations that would need to come out of the script would be:
OTKZ
OTK
OT
OTOSZ
OTOS
O
OL
OLKZ
OLK
I know this isn't even close to the exhaustive list of how many are actually possible but I can't figure out how to generate all the possible combinations.
Array
(
[0] => Array
(
[option_code] => O
[row_mod] => allowAll naWithOpON naWithOpOP
)
[1] => Array
(
[option_code] => ON
[row_mod] => allowAll naWithOpO naWithOpOP
)
[2] => Array
(
[option_code] => OP
[row_mod] => allowAll naWithOpO naWithOpON
)
[3] => Array
(
[option_code] => T
[row_mod] => allowAll naWithOpL
)
[4] => Array
(
[option_code] => L
[row_mod] => allowAll naWithOpT naWithCoilX
)
[5] => Array
(
[option_code] => K
[row_mod] => allow2 allow3 allow3P naWithOpOS
)
[6] => Array
(
[option_code] => OS
[row_mod] => allow2 allow3 allow3P naWithOpK
)
[7] => Array
(
[option_code] => Z
[row_mod] => allowAll naWithPreB
)
)
Basically I must start on the first option code and iterate down the list starting with the first given option 'O' then go to the next option 'ON' and check whether its valid and if it is then add it to the current combination. And so on and so forth building allowed combinations and storing them to an array.
User Interface example:
I created a jsfiddle that is interactive in the way that the options work together. Found here
I'm not sure what approach I should be taking but I have tried using recursive algorithms to no avail.
You have eight different options, which appear to be usable either zero or one times. Thus "not used" makes this nine. So you have
9 possibilities for the first option,
8 possibilities for the second (since the first cannot be reused),
7 possibilities for the third (since the first two cannot be reused)
...
Your basic number of options then, not counting the extra rules for option compatibility, is 9!, or 362880 combinations.
You could cycle through all possible combinations, and then write a function to test whether the combination is valid, given the rules for each type. Only list items for which the function returns true, and reject the others. It might be a bit slow, but it'll work.
To generate these combinations, I would visualise the sequence of options as a 9-digit number in base 9 (i.e. you have 9 things and they can each take one of 9 values, subject to limitations I note below). I think I am right in saying this is 99 (38742048910) different combinations, if there was no rule to disallow an option from being used twice.
Thus, just loop through these numbers in base 9, reject any numbers that have any duplicate digits, and of the ones that get through, convert it to an options string, and reject any that don't pass the per-options rule function. The list of items you are left with will be the full set of permitted combinations.

PHP array element starting with "<" weird behavior

I was trying to create some syntax for my application that uses $operator.$columnField as elements of an array for SELECT WHERE clause - something like selecting all ids less than 41 would have been
$parameters['where'] = array('<id'),
$parameters['fields'] = array(':id' => '41')
Then I would have parsed all ['where']s in order to determine the operator from the field itself. The main idea here is not if my way is a good way, given the fact that I can do it in a lot of different approaches. I am interested in the fact that it seems '<' plays some specific role if at the beginning of an array element of type string.
I noticed that there were some errors, so I started testing. Now can anyone tell me why
print_r(array('alfa', '<beta', 'gamma'));
echoes
Array (
[0] => alfa
[1] => gamma
)
Thanks in advance.
Later Edit: If the '<' character is followed by a space, the same does not apply any longer. It simply outputs
Array (
[0] => alfa
[1] => < beta
[2] => gamma
)
It actually works.. The < tag is intrepreted by the browser and it is hiding it from you.
Click Ctrl+U to view the source. You will see this..
Array
(
[0] => alfa
[1] => <beta
[2] => gamma
)
Well , if you want it for display purposes.. Do like this..
<?php
print_r(array_map('htmlentities',array('alfa', '<beta', 'gamma')));

Check, if array A contains all items from array B, when both arrays are multidimensional

I want to check, if array A contains all the items from array B (may contain others, but must contain all), when both arrays are multidimensional, i.e. can contains different variable types.
I've seen a lot (particularly this, this, this, this, this and this, also this, this and this as well). I've read PHP doc. Everything, that I checked, fails with "Array to string conversion" notice. Especially wen using array_intersect() or array_diff().
I'm using strict error checking, so notices actually holds further execution of entire script and are something, I don't generally like and want to avoid. Is it possible in this case?
My array A is:
Array
(
[0] => content/manage/index
[Content] => Array
(
[title] =>
[type] => 5
[category] =>
[recommended] =>
[featured] =>
[status] =>
[views] =>
[last_access_date] =>
[creation_date] =>
[modification_date] =>
[availability_date] =>
[author_id] =>
)
)
My array B is:
Array
(
[0] => /content/manage/index
[Content] => Array
(
[type] => 1
)
)
So, is there any way I can if I can use array_intersect on multidimensional arrays containing different variable types without getting notice?
My problem (and question) came out of misunderstanding, what "Array to string conversion" notice really means. In my case, it was trying to tell me, that I'm trying to walk multidimensional array with functions designed to be used on single dimension array.
Understanding that led me to a solution within few seconds. There are many of them here, on SO, but the one given by deceze here looked the best for me. So I adopted it into the form of such function:
function recursiveArrayIntersect($array1, $array2)
{
$array1 = array_intersect_key($array1, $array2);
foreach($array1 as $key=>&$value)
{
if(is_array($value)) $value = recursiveArrayIntersect($value, $array2[$key]);
}
return $array1;
}
I adopted it to my project and my way of coding, but all the credits still goes to deceze (his answer here)!
Now I can find an intersection of virtually any array, no matter what kind of variable types it contain and no matter of, how deep it is (how many subarrays it contains).

Fields get unwantedly concatenated in Salesforce SOQL query result. Developer nearly loses it

I'm probably missing something quite basic, but I'm getting very confused (and frustrated) with the results I get from my SOQL queries to the Salesforce API.
My query:
Select Id, FirstName, LastName FROM contact
The resulting object (as rendered by print_r):
stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPd7uAAC
[1] => 0032000000cPd7uAAC
)
[any] => BuzzAldrin
)
[1] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPt1zABC
[1] => 0032000000cPt1zABC
)
[any] => RonnieVanZant
)
[2] => stdClass Object
(
[type] => Contact
[Id] => Array
(
[0] => 0032000000cPb60AA
[1] => 0032000000cPb60AA
)
[any] => PollyJeanHarvey
)
)
[size] => 3
)
The first thing I don't get is why "Id" is an array. A strange quirk, but a workaround is not too hard.
The second thing bothers me endlessly more, though: I select for FirstName and LastName and what happens is they get concatenated and returned as a single string value for a field called "any". To avoid the "split it on uppercase letters" advice I already got from my colleagues, I provided an example with both a two-capital first name and a two-capital last name, and anyhow, in reality I need many more (and more formally unpredictable) fields, and they all get added to this "any" property.
Does anyone see what I'm doing wrong? Assuming it's not such a badly written API, that is?
Edit:
Said developer will now go sit in a corner for a few hours, repenting for not having checked for more recent versions of PHP Toolkit. Seems I was using 11.0, whereas there's already a version 20.0. Shame on me, shame on me indeed. Sorry for wasting your time.
The behavior you are seeing is mostly because of how PHP's SoapClient interprets the results from the API. If you call getLastResponse() on your API connection after you make the query() calls above, you'll see what the actual SOAP messages look like coming back from Salesforce.
As far as the Id array -- its not really an array, but it is listed twice per record (once for the record itself and once as a field), but PHP turns it into an array because it sees it twice. As far as the any, that's happening because PHP is not understanding the namespaced field tags correctly.
As it looks like you found, using the PHP Toolkit can help with these oddities and return sensible objects for you to work with. You might also want to consider looking at Salesforce's REST API, whose results can be directly consumed by json_decode(). For making the HTTP calls to Salesfore, you might be interested in this simple (almost standalone) REST client in a project of mine.

Turn X arrays into a single array

I want to change the format of this array to match another one. One was pulled from a database, and the other was made using space delimted user input and then explode();
So here are the arrays I want to change, this is from the database (mysql_fetch_array). It grabs the single field from all rows.
Array
(
[name] => daniel
)
Array
(
[name] => alex
)
Array
(
[name] => richard
)
And here is what I want it to look like. This was the output of the user submitted values, space delimted and using PHPs explode() function.
Array
(
[0] => daniel
[1] => alex
[2] => david
)
What I want to be able to do is have these in the same format so that I can compare the two. The end goal is to be able to compare the two arrays (user input and database results), and create a final array containing only values that the user has inputted that the database doesn't already contain.
So using the data above this would be my final array:
Array
(
[0] => david
)
I would really appreciate help with the first bit, and if anyone else has a better way to achieve the end goal that would be a great extra bonus! (I get the feeling it might be easier to do this with SQL queries but I'm really confusing myself with these arrays)
Thanks!
array_diff($user_array, $database_array);
You can construct the $database_array like this:
$database_array = array();
//assuming each db record has only one value
//while there are still results in the db, do:
$database_array[] = reset($fetched_record);
See array_diff.

Categories