ElasticSearch - order by score and location - php

I use elasticsearch. I need to find the results by word and sort them by deception (_score), and then by distance (_geo_distance)
By coincidence of phrases, I find the following sensations and sort them out by coincidence (_score).
Array
(
[took] => 23
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array
(
[total] => 469
[max_score] => 5.350864
[hits] => Array
(
[0] => Array
(
[_index] => salon
[_type] => services
[_id] => 7686
[_score] => 5.350864
[_source] => Array
(
[service_name] => Found a string 1
[service_id] => 10493
[location] => Array
(
[lat] => 55.701328
[lon] => 37.507412
)
)
)
[1] => Array
(
[_index] => salon
[_type] => services
[_id] => 8350
[_score] => 5.350864
[_source] => Array
(
[service_name] => Found a string 2
[service_id] => 11171
[location] => Array
(
[lat] => 55.869915
[lon] => 37.613728
)
)
)
[2] => Array
(
[_index] => salon
[_type] => services
[_id] => 14883
[_score] => 5.237593
[_source] => Array
(
[service_name] => Found a string 3
[service_id] => 17851
[location] => Array
(
[lat] => 55.691734
[lon] => 37.728164
)
)
)
...
But I do not understand how to add more and sorting by coordinates, for example, so that would be sorted by the nearest to 55.69,37.72
Here is my search code in elasticsearch:
require 'vendor/autoload.php';
function searchES($word) {
$client = \Elasticsearch\ClientBuilder::create()->setHosts([ES_HOST])->build();
$IDs = [];
$params = [];
$params['index'] = ES_INDEX;
if ($client->indices()->exists($params)) {
$params['type'] = ES_TYPE;
$params['size'] = 10000;
$params['body']['sort'] = ['_score' => 'desc'];
$params['body']['query']['match']['service_name'] = trim($word);
$result = $client->search($params);
if ($result['hits']['total'] > 0) {
$result = $result['hits']['hits'];
foreach ($result as $val) {
$k = $val['_source']['service_id'];
$IDs[$k] = $val['_source']['service_name'];
}
}
}
return $IDs;
}
Params:
Array
(
[index] => salon
[body] => Array
(
[settings] => Array
(
[analysis] => Array
(
[filter] => Array
(
[ru_stop] => Array
(
[type] => stop
[stopwords] => _russian_
)
[ru_stemmer] => Array
(
[type] => stemmer
[language] => russian
)
)
[analyzer] => Array
(
[default] => Array
(
[tokenizer] => standard
[filter] => Array
(
[0] => lowercase
[1] => ru_stop
[2] => ru_stemmer
)
)
)
)
)
[mappings] => Array
(
[_default_] => Array
(
[properties] => Array
(
[service_name] => Array
(
[type] => string
[analyzer] => default
)
)
)
)
)
)

Related

How do I get the array block from within the array?

I want to convert the xml data that I have generated from my system into an array array and send it to the API system of the platform I sell individually.
I'd appreciate it if you could review and support.
my array data example:
Array
(
[product] => Array
(
[0] => Array
(
[productSellerCode] => bebe27
[title] => Uzun Kollu Renkli Uyku Tulum Body Zıbın 6 Aylık Bebek
[subtitle] => Stoktan hızlı kargo
[description] => Bebeğiniz İçin Özel Günlerde ve Günlük Olarak Kullanabileceği Mükemmel Takım.Kaliteli Şık ve Zarif Olan Bu Ürünü Sizlere Sunuyoruz.%100 Pamuk olan bu ürünü Bebeğinize Güvenle Giydirebilirsiniz Dokusu Yumuşaktır.
[category] => Array
(
[id] => 1000053
)
[price] => 48.472
[domestic] => false
[currencyType] => 1
[images] => Array
(
[image] => Array
(
[url] => https://tuccar.com.tr/image/catalog/bebe/IMG_7976.JPG
[order] => 1
)
)
[attributes] => Array
(
[attribute] => Array
(
[0] => Array
(
[name] => Beden
[value] => 3-6 Ay
)
[1] => Array
(
[name] => Marka
[value] => DİĞER
)
)
)
[saleStartDate] => Array
(
)
[saleEndDate] => Array
(
)
[productionDate] => Array
(
)
[expirationDate] => Array
(
)
[productCondition] => 1
[preparingDay] => 2
[discount] => Array
(
[startDate] => Array
(
)
[endDate] => Array
(
)
[type] => Array
(
)
[value] => Array
(
)
)
[shipmentTemplate] => kargo-bizden
[stockItems] => Array
(
[stockItem] => Array
(
[quantity] => 1
[sellerStockCode] => Array
(
)
[attributes] => Array
(
[attribute] => Array
(
[name] => Array
(
)
[value] => Array
(
)
)
)
[optionPrice] => Array
(
)
)
)
)
[1] => Array
(
[productSellerCode] => MS-S010
[title] => 10 cm Samur Kase
[subtitle] => Stoktan hızlı kargo
[description] => Ölçü: 10 cmÜlkemizde, el işçiliği şeklinde üretilmiştir.
[category] => Array
(
[id] => 1016108
)
[price] => 31.426
[domestic] => false
[currencyType] => 1
[images] => Array
(
[image] => Array
(
[url] => https://tuccar.com.tr/image/catalog/MS/MS-S009-1.jpg
[order] => 1
)
)
[attributes] => Array
(
[attribute] => Array
(
[0] => Array
(
[name] => Marka
[value] => DİĞER
)
[1] => Array
(
[name] => Materyal
[value] => Seramik
)
)
)
[saleStartDate] => Array
(
)
[saleEndDate] => Array
(
)
[productionDate] => Array
(
)
[expirationDate] => Array
(
)
[productCondition] => 1
[preparingDay] => 3
[discount] => Array
(
[startDate] => Array
(
)
[endDate] => Array
(
)
[type] => Array
(
)
[value] => Array
(
)
)
[shipmentTemplate] => kargo-bizden
[stockItems] => Array
(
[stockItem] => Array
(
[quantity] => 10
[sellerStockCode] => Array
(
)
[attributes] => Array
(
[attribute] => Array
(
[name] => Array
(
)
[value] => Array
(
)
)
)
[optionPrice] => Array
(
)
)
)
)
I want to post an array block here every time.
(
[productSellerCode] => MS-S010
[title] => 10 cm Samur Kase
[subtitle] => Stoktan hızlı kargo
[description] => Ölçü: 10 cmÜlkemizde, el işçiliği şeklinde üretilmiştir.
[category] => Array
(
[id] => 1016108
)
[price] => 31.426
[domestic] => false
[currencyType] => 1
[images] => Array
(
[image] => Array
(
[url] => https://tuccar.com.tr/image/catalog/MS/MS-S009-1.jpg
[order] => 1
)
)
[attributes] => Array
(
[attribute] => Array
(
[0] => Array
(
[name] => Marka
[value] => DİĞER
)
[1] => Array
(
[name] => Materyal
[value] => Seramik
)
)
)
[saleStartDate] => Array
(
)
[saleEndDate] => Array
(
)
[productionDate] => Array
(
)
[expirationDate] => Array
(
)
[productCondition] => 1
[preparingDay] => 3
[discount] => Array
(
[startDate] => Array
(
)
[endDate] => Array
(
)
[type] => Array
(
)
[value] => Array
(
)
)
[shipmentTemplate] => kargo-bizden
[stockItems] => Array
(
[stockItem] => Array
(
[quantity] => 10
[sellerStockCode] => Array
(
)
[attributes] => Array
(
[attribute] => Array
(
[name] => Array
(
)
[value] => Array
(
)
)
)
[optionPrice] => Array
(
)
)
)
)
I think I'm making a mistake in foreach operations.
I can send 1 product as below, I need to send all products in order.
<?php
include "class.n11.php";
$n11Params = [
'appKey' => 'my-api-key',
'appSecret' => 'my-api-secret'
];
$n11 = new N11($n11Params);
$xmlfile = file_get_contents("https://tuccar.com.tr/API/n11/100urun.php");
$new = simplexml_load_string($xmlfile);
$con = json_encode($new,JSON_UNESCAPED_UNICODE);
$newArr = json_decode($con, true);
foreach ($newArr as &$urun) {
$productSellerCode = $urun[$i]->productSellerCode;
$title = $urun[$i]->title;
$description = $urun[$i]->description;
$categoryid = $urun[$i]->category->id;
$price = $urun[$i]->price;
$preparingDay = $urun[$i]->preparingDay;
$quantity = $urun[$i]->quantity;
$saveProduct = $n11->SaveProduct(
[
'productSellerCode' => $productSellerCode,
'title' => $title,
'subtitle' => 'Stoktan hızlı kargo',
'description' => $description,
'category' =>
[
'id' => $categoryid
],
'attributes' =>
[
'attribute' =>
foreach($urun->attributes as $attribute){
'name' => $attribute['name'],
'value' => $attribute['value']
}
],
'price' => $price,
'domestic' => false,
'currencyType' => 'TL',
'images' =>
[
'image' =>
foreach($urun->images as $image){
'url' => $image['url'],
'order' => $image['order']
}
],
'saleStartDate' => '',
'saleEndDate' => '',
'productionDate' => '',
'expirationDate' => '',
'productCondition' => '1',
'preparingDay' => $preparingDay,
'discount' => '',
'shipmentTemplate' => 'kargo-bizden',
'stockItems' =>
[
'stockItem' =>
[
'quantity' => $quantity,
'sellerStockCode' => '',
'attributes' =>
[
'attribute' => []
],
'optionPrice' => ''
]
]
]
);
} var_dump($saveProduct);
#RiggsFolly, It worked that way yes but only the first ranked product was sent. I couldn't do the loop.
<?php
include "class.n11.php";
$n11Params = ['appKey' => 'my-api-key', 'appSecret' => 'my-api-secret'];
$n11 = new N11($n11Params);
$xmlfile = file_get_contents("https://tuccar.com.tr/API/n11/100urun.php");
$new = simplexml_load_string($xmlfile);
$con = json_encode($new,JSON_UNESCAPED_UNICODE);
$newArr = json_decode($con, true);
foreach ($newArr['product'] as $product) {
$saveProduct = $n11->SaveProduct($product);
}
var_dump($saveProduct);
I succeeded with the following code.
var_dump($products);
foreach($products as $indis=>$degeri){
foreach($degeri as $icindekiler){
$saveProduct = $n11->SaveProduct($icindekiler);
}
}
var_dump($saveProduct);
simply start you foreach one step down into the array i.e. from $newArr['product'] and each array you process from there will be the array you want to pass as the parameter.
foreach ($newArr['product'] as $product) {
$saveProduct = $n11->SaveProduct($product);
}
Also this may or may not actually exist in your real code but this array is not correctly coded, it has a missing '
$n11Params = [
'appKey' => 'my-api-key', // missing quote added
'appSecret' => 'my-api-secret'
];

Get nested values from JSON from jstree with PHP

I use jstree to store a tree structure using JSON. My example structure looks like this:
After using
$treeJSONdecoded = json_decode($treeJSON, true);
my JSON looks like this:
[0] => Array (
[id] => j1_1
[text] => Release1
[icon] => 1
[li_attr] => Array (
[id] => j1_1
)
[a_attr] => Array (
[href] => #
[id] => j1_1_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] => 1
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_3
[text] => List of features
[icon] => 1
[li_attr] => Array (
[id] => j1_3
)
[a_attr] => Array (
[href] => #
[id] => j1_3_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] =>
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_9
[text] => feature1
[icon] => 1
[li_attr] => Array (
[id] => j1_9
)
[a_attr] => Array (
[href] => #
[id] => j1_9_anchor
)
[state] => Array (
[loaded] => 1
[opened] =>
)
[data] => Array ( )
[children] => Array ( )
[type] => default
)
)
[type] => default
)
[1 => Array ( id] => j1_2
[text] => List of documents
[icon] => 1
[li_attr] => Array (
[id] => j1_2
)
[a_attr] => Array (
[href] => #
[id] => j1_2_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] =>
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_5
[text] => document1
[icon] => 1
[li_attr] => Array (
[id] => j1_5
)
[a_attr] => Array (
[href] => #
[id] => j1_5_anchor
)
[state] => Array (
[loaded] => 1
[opened] =>
)
[data] => Array ( )
[children] => Array ( )
[type] => default
)
)
[type] => default )
)
[type] => default
)
How do I iterate through the whole JSON to get 'text' values and have an array like:
{"Release1", "List of features", ... , "document1"}
assuming that I don't know how many levels there are. I tried something like
foreach($treeJSONdecoded as $val){
echo $val['text'];
}
just to see what I can fetch but it doesn't seem to work.
Thanks #Scuzzy, this got me what I wanted:
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($treeJSON, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
$newArray = [];
foreach ($jsonIterator as $key => $val) {
if(!is_array($val) && $key == 'text') {
array_push($newArray, $val);
}
}

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 do I add an additional child array recursively within last array in PHP

I am attempting to add another child array to the last child array.
Here is a sample of my array,
Array
(
[Auto-Trail] => Array
(
[name] => Auto-Trail
[children] => Array
(
[2001] => Array
(
[name] => 2001
[children] => Array
(
[Tracker] => Array
(
[name] => Tracker
[children] => Array
(
[CK] => Array
(
[name] => CK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
)
)
[EK] => Array
(
[name] => EK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
)
)
)
)
[Cheyenne] => Array
(
[name] => Cheyenne
[children] => Array
(
[630S] => Array
(
[name] => 630S
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[630] => Array
(
[name] => 630
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[634] => Array
(
[name] => 634
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
[634U] => Array
(
[name] => 634U
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
)
[Merc] => Array
(
[name] => Merc
[children] => Array
(
[313] => Array
(
[name] => 313
)
[316] => Array
(
[name] => 316
)
)
)
)
)
This is the array I wish to add to every last element,
For example, I need to add the following to [CK][children][Fiat][children] (and every last children's children) - this array will be static, every last child will use the same array.
[BOF] => Array
(
[name] => Furniture
)
[CHA] => Array
(
[name] => Chassis
)
So ... ][CK][children][Fiat] would become the following,
[CK] => Array
(
[name] => CK
[children] => Array
(
[Fiat] => Array
(
[name] => Fiat
[children] => Array
(
[BOF] => Array
(
[name] => Furniture
)
[CHA] => Array
(
[name] => Chassis
)
)
)
)
)
Please note, Not every last child is the same indentation level, it could be 10 more children indentations. It must use the last array within the array of each array.
Apologies if it's hard to understand, I'm struggling to word exactly the way I need it to work.
Thank you for your time and effort.
check out this solution (quick'n'dirty):
function injectArray(array $target, array $inject, $depth = 0){
$hasChildrenKey = array_key_exists('children', $target);
$hasChildren = $hasChildrenKey && !empty($target['children']);
if($depth % 2 == 0){
foreach($target as $k => $v)
if(is_array($v))
$target[$k] = injectArray($v, $inject, $depth+1);
}
elseif(!$hasChildren){
if(!$hasChildrenKey)
$target['children'] = array();
$target['children'] = array_merge_recursive($target['children'], $inject);
}
else if($hasChildrenKey){
$target['children'] = injectArray($target['children'], $inject, $depth+1);
}
return $target;
};
$testArray = array(
'Auto-Trail' => array('name' => 'asdf',
'children' => array(
'2001' => array('name' => '2001',
'children' => array(
'Tracker' => array('name' => 'Tracker',
'children' => array(
'CK' => array('name' => 'CK',
'children' => array(
'Fiat' => array('name' => 'Fiat')
)
)
)
)
)
)
)
)
);
$testInjectArray = array('BOF' => array('name' => 'Furniture'), 'CHA' => array('name' => 'Chassis'));
$result = injectArray($testArray, $testInjectArray);
print_r($result);

adding up value of array and getting the average

I have an array that looks similar to this,
[4] => Common_Model Object
(
[id] => 4
[name] =>
[date_created] =>
[last_updated] =>
[user_id_updated] =>
[_table] =>
[_aliases] => Array
(
[id] => 4
[name] =>
[date_created] =>
[date_updated] =>
[user_id_updated] =>
[rating] => 3
[recipe_id] => 5
)
[_nonDBAliases] => Array
(
)
[_default] => Array
(
)
[_related] => Array
(
)
[_enums] =>
[_alsoDelete] => Array
(
)
[_readOnly] => Array
(
[0] => date_updated
)
[_valArgs] => Array
(
)
[_valArgsHash] => Array
(
[default] => Array
(
)
)
[_valAliases] => Array
(
)
[_extraData] => Array
(
)
[_inputs] => Array
(
)
[_tableName] => jm_ratings
[_tablePrefix] =>
[_niceDateUpdated] => 1st Jan 70
[_niceDateCreated] => 1st Jan 70
[_fetchAdminData] =>
[_mCache] =>
[_assets] => Array
(
)
)
[3] => Common_Model Object
(
[id] => 3
[name] =>
[date_created] =>
[last_updated] =>
[user_id_updated] =>
[_table] =>
[_aliases] => Array
(
[id] => 3
[name] =>
[date_created] =>
[date_updated] =>
[user_id_updated] =>
[rating] => 1
[recipe_id] => 5
)
[_nonDBAliases] => Array
(
)
[_default] => Array
(
)
[_related] => Array
(
)
[_enums] =>
[_alsoDelete] => Array
(
)
[_readOnly] => Array
(
[0] => date_updated
)
[_valArgs] => Array
(
)
[_valArgsHash] => Array
(
[default] => Array
(
)
)
[_valAliases] => Array
(
)
[_extraData] => Array
(
)
[_inputs] => Array
(
)
[_tableName] => jm_ratings
[_tablePrefix] =>
[_niceDateUpdated] => 1st Jan 70
[_niceDateCreated] => 1st Jan 70
[_fetchAdminData] =>
[_mCache] =>
[_assets] => Array
(
)
)
[2] => Common_Model Object
(
[id] => 2
[name] =>
[date_created] =>
[last_updated] =>
[user_id_updated] =>
[_table] =>
[_aliases] => Array
(
[id] => 2
[name] =>
[date_created] =>
[date_updated] =>
[user_id_updated] =>
[rating] => 1
[recipe_id] => 5
)
[_nonDBAliases] => Array
(
)
[_default] => Array
(
)
[_related] => Array
(
)
[_enums] =>
[_alsoDelete] => Array
(
)
[_readOnly] => Array
(
[0] => date_updated
)
[_valArgs] => Array
(
)
[_valArgsHash] => Array
(
[default] => Array
(
)
)
[_valAliases] => Array
(
)
[_extraData] => Array
(
)
[_inputs] => Array
(
)
[_tableName] => jm_ratings
[_tablePrefix] =>
[_niceDateUpdated] => 1st Jan 70
[_niceDateCreated] => 1st Jan 70
[_fetchAdminData] =>
[_mCache] =>
[_assets] => Array
(
)
)
I wanting to add up the [rating] and get the mean average. But I dont know how do this with PHP, my attempt looks like this,
<?php
foreach ($rt as $rating) {
$total = $rating->rating + $rating->rating
}
$total / count($rt);
?>
Try this:
$total = 0;
foreach($rt as $elem) {
$total += $elem->_aliases['rating'];
}
echo sprintf("Average: %d", $total/count($rt));
<?php
$total = 0;
foreach ($rt as $obj) {
$total += $obj->_aliases['rating'];
}
print $total / count($rt);
?>

Categories