php call sphinx, total found is 2, but matches is empty, why? - php

php call sphinx, total found is 2, but matches is empty, why?
php package: https://pecl.php.net/package/sphinx version:1.3.3
sphinx version:2.0.1
when I query('#author tairyao', '*'), the result is:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => author
[1] => title
[2] => description
)
[attrs] => Array
(
)
[matches] => Array
(
[102373] => Array
(
[weight] => 7702
[attrs] => Array
(
)
)
[102428] => Array
(
[weight] => 7702
[attrs] => Array
(
)
)
)
[total] => 2
[total_found] => 2
[time] => 0
[words] => Array
(
[tairyao] => Array
(
[docs] => 4
[hits] => 4
)
)
)
The total and total_found are all 2, why attrs is empty?

Related

How to unset specific array in nested foreach?

I have an output array like this:
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[Dagadu Bocah] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[HirukPikuk] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[DGD] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[DGD] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
in my expectations I can use the loop as I asked earlier in this question by doing repetitions in such a way as to delete certain arrays to eliminate different parts of the array which is an array that is above the array which has three items namely 'item', 'count' and 'child' and how to produce arrays like this from the array above?
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[0] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[0] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[0] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[1] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[2] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
It seems you want to convert the child arrays from associative arrays to indexed arrays.
Assuming your data is called $data, here is a recursive function you could use:
function convert(&$data) {
foreach ($data as $row) {
if(isset($row["child"])) {
$row["child"] = array_values($row["child"]);
convert($row["child"]);
}
}
}
// call it for each row
foreach($data as $row) convert($row);

Count subarray elements and sum deep subarray elements

I'm working with an API that returns a JSON object which I then decode into an array using
I need to get a count of all the [data] items in the array, and then generate a total of all the [Total] values contained in each
Here's my array:-
Array
(
[errorCode] => 0
[result] => OK
[data] => Array
(
[0] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 128.97
[Customers::Company] => A B C Lock & Key
[Status] => Paid
[Date Payment] => 11/30/2017
)
[portalData] => Array
(
)
[recordId] => 1
[modId] => 4
)
[1] => Array
(
[fieldData] => Array
(
[Due Date] => 12/01/2017
[Date] => 12/01/2017
[Total] => 256
[Customers::Company] => Kim Peacock Beringhause
[Status] => Paid
[Date Payment] => 12/01/2017
)
[portalData] => Array
(
)
[recordId] => 2
[modId] => 3
)
[2] => Array
(
[fieldData] => Array
(
[Due Date] => 11/30/2017
[Date] => 11/30/2017
[Total] => 1880
[Customers::Company] => Norton, Robert L Esq
[Status] => Unpaid Overdue
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 3
[modId] => 0
)
[3] => Array
(
[fieldData] => Array
(
[Due Date] => 12/22/2017
[Date] => 12/22/2017
[Total] => 1278
[Customers::Company] => Shapiro, Mark R Esq
[Status] => Unpaid
[Date Payment] =>
)
[portalData] => Array
(
)
[recordId] => 10
[modId] => 1
)
)
)
I've tried:
count($array)
which returns 3 which I can see are the count of the 3 items in that parent array (errorCode, result and data), but I can't work out how to focus on the data only.
The count value I'm after is 4 and the total value is 3542.97.
For count you need to do:
count($array['data']);
And for getting sum of all Total index values:
echo array_sum(
array_column(
array_column(
$array['data'],
'fieldData'
),
'Total'
)
);
Reference:- PHP: array_column - Manual

Compare values of two multidimentional array and insert if not exits

I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){

retrieving data from a php object

I am trying to access data inside a php object. I am confused about what the exact stynax is. Here is the output of the object:
stdClass Object ( [vid] => 10 [uid] => 1 [title] => Beachhead Don [log] =>
[status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [ds_switch] => [nid] =>
10 [type] => product [language] => und [created] => 1357668665 [changed] => 1358198386
[tnid] => 0 [translate] => 0 [revision_timestamp] => 1358198386 [revision_uid] => 1
[model] => B 3000 [list_price] => 0.00000 [cost] => 0.00000 [sell_price] => 1.00000
[weight] => 0 [weight_units] => lb [length] => 0 [width] => 0 [height] => 0
[length_units] => in [pkg_qty] => 1 [default_qty] => 1 [unique_hash] =>
2eec0fcc8483f3a3541870fb24223296 [ordering] => 0 [shippable] => 0 [price] => 1.00000
[body] => Array ( [und] => Array ( [0] => Array ( [value] => Description info
))))))))))))))))))))))))))))))))))))))0 [summary] => [format] => filtered_html
[safe_value] =>Description info ))))))))))))))))))))))))))))))))))))))0[safe_summary]
=> ) ) ) [uc_product_image] => Array ( [und] => Array ( [0] => Array ( [fid] => 11
[alt] => [title] => [width] => 90 [height] => 120 [uid] => 1 [filename] =>
beachhead_don.jpg [uri] => public://beachhead_don.jpg [filemime] => image/jpeg
[filesize] => 14342 [status] => 1 [timestamp] => 1357668665 [rdf_mapping] => Array ( )
) ) ) [field_author] => Array ( [und] => Array ( [0] => Array ( [value] => John B.
Romesier [format] => [safe_value] => John B. Romesier ) ) ) [field_publisher] => Array
( [und] => Array ( [0] => Array ( [value] => Unknown [format] => [safe_value] =>
Unknown ) ) ) [field_release_date] => Array ( [und] => Array ( [0] => Array ( [value]
=> Mar 2012 [format] => [safe_value] => Mar 2012 ) ) ) [field_number_of_pages] => Array
( [und] => Array ( [0] => Array ( [value] => 123 ) ) ) [field_catagory] => Array (
[und] => Array ( [0] => Array ( [tid] => 4 ) ) ) [field_book_type] => Array ( [und] => Array ( [0] => Array ( [tid] => 1 ) ) ) [field_isbn] => Array ( ) [field_about_the_author] => Array ( [und] => Array ( [0] => Array ( [value] => Beachhed Don authoer............................ [format] => [safe_value] => Beachhed Don authoer............................ ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1357668665 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 )
I am trying to retrive the [value] of Description info ))))))))))))))))))))))))))))))))))))))0
I tried this
<?php
$node = menu_get_object(); //drupal code
if ( !empty($node) ) {
print $node ->body=>und=>0=>value;
}?>
and got an error about the '=' sign. What is the proper way to get my data?
[body] and [und] are arrays, so access them like this:
echo $node->body['und'][0]['value'];
It should be:
$node->body['und'][0]['value']
'Body' and 'und' are Arrays and need to be accessed with array syntax([]).
Accessing these values directly is sometimes frowned upon in drupal. You may want to look into: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_render/7

How to extract Sphinx group by numbers from result set?

I need some help on Sphinx MySql index group by.
I have a product table where I have product ids and corresponding retailer ids. What I want to do is to create a simple list to show the retailers' names along with how many products it has in the table:
standard SQL: select retailerid, count(productid)as cnt from tblproducts
Sphinxs: select * from tblproducts1 group by retailerid
I am using Sphinx 2.0.1-beta
The reason I want to use Sphinx is, because the number of products will be filtered by a complex match full text query, on product name and description etc.
Now my question is, how do I get these numbers/ids from the result returned by Sphinx? As I am using php to display results, I would like if somebody can show me some useful php code to extract these numbers from the result set. The result array returned by Sphinx is confusing me even more; I do not see any reference to the product count anywhere!
I am sure that Sphinx returns these numbers as it supports group by and count, but how do I extract the numbers from the result set?
Below is from my config file:
sql_query = \
SELECT ProductId, ProductName, ProductModel, ProductDesc, ProductManf, ProductHeader, \
ProductPrice, ProductPrePrice, ProductFetchDate, m.MerchantId, m.MerchantActive FROM tblproducts p \
inner join tblmerchantlist m on p.MerchantId=m.MerchantId
sql_attr_uint = MerchantActive
sql_attr_float = ProductPrice
sql_attr_float = ProductPrePrice
sql_group_column = MerchantId
sql_query_info = SELECT * FROM tblproducts WHERE ProductId=$id
UPDATE
I would expect so, but I don't see the reference to count in the below resultset. I must be making some mistakes somewhere:
Array ( [error] => [warning] => [status] => 0 [fields] =>
Array ( [0] => productname [1] => productmodel [2] => productdesc [3] => productmanf [4] => productheader [5] => productfetchdate ) [attrs] =>
Array ( [productprice] => 5 [productpreprice] => 5 [merchantid] => 1 [merchantactive] => 1 ) [matches] =>
Array ( [0] =>
Array ( [id] => 694173 [weight] => 396305 [attrs] =>
Array ( [productprice] => 1568.48999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [1] =>
Array ( [id] => 901921 [weight] => 396305 [attrs] =>
Array ( [productprice] => 1533.48999023 [productpreprice] => 1536.98999023 [merchantid] => 12 [merchantactive] => 1 ) ) [2] =>
Array ( [id] => 302573 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1059.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [3] =>
Array ( [id] => 302579 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1179.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [4] =>
Array ( [id] => 302592 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1429.48999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [5] =>
Array ( [id] => 302595 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1592.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [6] =>
Array ( [id] => 302597 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1129.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [7] =>
Array ( [id] => 406798 [weight] => 249249 [attrs] =>
Array ( [productprice] => 2419.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [8] =>
Array ( [id] => 407480 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1287.48999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) [9] =>
Array ( [id] => 693715 [weight] => 249249 [attrs] =>
Array ( [productprice] => 1234.98999023 [productpreprice] => 0 [merchantid] => 12 [merchantactive] => 1 ) ) ) [total] => 29301 [total_found] => 29301 [time] => 0.137 [words] =>
Array ( [select] =>
Array ( [docs] => 390 [hits] => 462 ) [from] =>
Array ( [docs] => 4332 [hits] => 4637 ) [tblproducts1] =>
Array ( [docs] => 0 [hits] => 0 ) [where] =>
Array ( [docs] => 395 [hits] => 448 ) [match] =>
Array ( [docs] => 108 [hits] => 111 ) [cyberpowerpc] =>
Array ( [docs] => 66 [hits] => 132 ) [gamer] =>
Array ( [docs] => 307 [hits] => 715 ) [xtreme] =>
Array ( [docs] => 410 [hits] => 725 ) [1310lq] =>
Array ( [docs] => 2 [hits] => 6 ) [in] =>
Array ( [docs] => 16196 [hits] => 19786 ) [canada] =>
Array ( [docs] => 1146 [hits] => 1200 ) [group] =>
Array ( [docs] => 5716 [hits] => 5732 ) [by] =>
Array ( [docs] => 2143 [hits] => 2289 ) [merchantid] =>
Array ( [docs] => 0 [hits] => 0 )
)
)
There is a magic attribute #count that added to your result set for that query
select * from tblproducts1 group by retailerid
The attribute list in result set looks like
id, weight, MerchantActive, ... #groupby, #count
In case you use compat_sphinxql_magic=0 option in searchd section you have explicitly define and alias all needed attributes like
select *, weight(*) as w, count(*) as c from tblproducts1 group by retailerid
The attribute list in result set would look like
id, MerchantActive, ... w, c

Categories