From start I have a list of orders in the following way:
(
[product_id] => 1094
[total_weight] => 0.0390
[qty_ordered] => 10.0000
[category_id] => 12
)
What I want is to sum the values for total_weight and qty_ordered based on category_id
I have managed to get the array in the below manner now using a foreach. But I need to aggregate the values somehow.
Array
(
[14] => Array
(
[0] => Array
(
[product_id] => 1099
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
[1] => Array
(
[product_id] => 1095
[total_weight] => 0.0395
[qty_ordered] => 10.0000
)
)
[12] => Array
(
[0] => Array
(
[product_id] => 1094
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
)
[15] => Array
(
[0] => Array
(
[product_id] => 742
[total_weight] => 0.1250
[qty_ordered] => 1.0000
)
)
)
So ideally I would the output to be like with below with as little code as possible:
Array
(
[14] => Array
(
[total_weight] => 0.0785
[qty_ordered] => 20.0000
)
[12] => Array
(
[total_weight] => 0.0390
[qty_ordered] => 10.0000
)
[15] => Array
(
[total_weight] => 0.1250
[qty_ordered] => 1.0000
)
)
I was able to sort it in the below way.
foreach($retval as $val) {
$return[$val['category_id']]['qty_ordered'] += $val['qty_ordered'];
$return[$val['category_id']]['row_total'] += $val['row_total'];
}
Related
I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}
Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}
I'm using sphinx php api to sort search results. I want to give more weight to title and less to content, I've tried setting setFieldWeights
$sp->setFieldWeights(array('title'=>10,'full_story'=>1)); but it's not working at all. not matter how i change the weight, results stays the same.
print_r with "test" as a search word and setFieldWeights(array('title'=>10,'full_story'=>1));
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => full_story [2] => category ) [attrs] => Array ( [date] => 2 ) [matches] => Array ( [0] => Array ( [id] => 1933 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [1] => Array ( [id] => 2824 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [2] => Array ( [id] => 2860 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [3] => Array ( [id] => 2865 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [4] => Array ( [id] => 5720 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [5] => Array ( [id] => 6963 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [6] => Array ( [id] => 8190 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [7] => Array ( [id] => 8591 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [8] => Array ( [id] => 10114 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [9] => Array ( [id] => 10157 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [10] => Array ( [id] => 11162 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [11] => Array ( [id] => 11659 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [12] => Array ( [id] => 13219 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [13] => Array ( [id] => 13220 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [14] => Array ( [id] => 13565 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [15] => Array ( [id] => 13576 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [16] => Array ( [id] => 14801 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [17] => Array ( [id] => 14857 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [18] => Array ( [id] => 17311 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [19] => Array ( [id] => 17511 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [20] => Array ( [id] => 18131 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [21] => Array ( [id] => 19171 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [22] => Array ( [id] => 21134 [weight] => 10 [attrs] => Array ( [date] => 2012 ) ) [23] => Array ( [id] => 23007 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [24] => Array ( [id] => 23230 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [25] => Array ( [id] => 23460 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [26] => Array ( [id] => 23473 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [27] => Array ( [id] => 25609 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [28] => Array ( [id] => 28093 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [29] => Array ( [id] => 28175 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [30] => Array ( [id] => 30364 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [31] => Array ( [id] => 30506 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [32] => Array ( [id] => 31439 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [33] => Array ( [id] => 31668 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [34] => Array ( [id] => 32017 [weight] => 10 [attrs] => Array ( [date] => 2013 ) ) [35] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [36] => Array ( [id] => 5362 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [37] => Array ( [id] => 9175 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [38] => Array ( [id] => 13817 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [39] => Array ( [id] => 14132 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [40] => Array ( [id] => 14658 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [41] => Array ( [id] => 15792 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [42] => Array ( [id] => 16615 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [43] => Array ( [id] => 18177 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [44] => Array ( [id] => 18581 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [45] => Array ( [id] => 18618 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [46] => Array ( [id] => 18665 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [47] => Array ( [id] => 18718 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [48] => Array ( [id] => 19096 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [49] => Array ( [id] => 19128 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [50] => Array ( [id] => 19464 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [51] => Array ( [id] => 20014 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [52] => Array ( [id] => 20144 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [53] => Array ( [id] => 20306 [weight] => 1 [attrs] => Array ( [date] => 2012 ) ) [54] => Array ( [id] => 22604 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [55] => Array ( [id] => 23870 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [56] => Array ( [id] => 23994 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [57] => Array ( [id] => 24507 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [58] => Array ( [id] => 24617 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [59] => Array ( [id] => 25043 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [60] => Array ( [id] => 25350 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [61] => Array ( [id] => 26063 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [62] => Array ( [id] => 26614 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [63] => Array ( [id] => 26819 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [64] => Array ( [id] => 28292 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [65] => Array ( [id] => 29155 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [66] => Array ( [id] => 30854 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [67] => Array ( [id] => 31566 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [68] => Array ( [id] => 31704 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) [69] => Array ( [id] => 32092 [weight] => 1 [attrs] => Array ( [date] => 2013 ) ) ) [total] => 70 [total_found] => 70 [time] => 0,017 [words] => Array ( [*test*] => Array ( [docs] => 70 [hits] => 70 ) ) )
print_r with "test" as a search word and setFieldWeights(array('title'=>10000,'full_story'=>1));
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => full_story [2] => category [3] => date ) [attrs] => Array ( ) [matches] => Array ( [0] => Array ( [id] => 1933 [weight] => 10000 [attrs] => Array ( ) ) [1] => Array ( [id] => 2824 [weight] => 10000 [attrs] => Array ( ) ) [2] => Array ( [id] => 2860 [weight] => 10000 [attrs] => Array ( ) ) [3] => Array ( [id] => 2865 [weight] => 10000 [attrs] => Array ( ) ) [4] => Array ( [id] => 5720 [weight] => 10000 [attrs] => Array ( ) ) [5] => Array ( [id] => 6963 [weight] => 10000 [attrs] => Array ( ) ) [6] => Array ( [id] => 8190 [weight] => 10000 [attrs] => Array ( ) ) [7] => Array ( [id] => 8591 [weight] => 10000 [attrs] => Array ( ) ) [8] => Array ( [id] => 10114 [weight] => 10000 [attrs] => Array ( ) ) [9] => Array ( [id] => 10157 [weight] => 10000 [attrs] => Array ( ) ) [10] => Array ( [id] => 11162 [weight] => 10000 [attrs] => Array ( ) ) [11] => Array ( [id] => 11659 [weight] => 10000 [attrs] => Array ( ) ) [12] => Array ( [id] => 13219 [weight] => 10000 [attrs] => Array ( ) ) [13] => Array ( [id] => 13220 [weight] => 10000 [attrs] => Array ( ) ) [14] => Array ( [id] => 13565 [weight] => 10000 [attrs] => Array ( ) ) [15] => Array ( [id] => 13576 [weight] => 10000 [attrs] => Array ( ) ) [16] => Array ( [id] => 14801 [weight] => 10000 [attrs] => Array ( ) ) [17] => Array ( [id] => 14857 [weight] => 10000 [attrs] => Array ( ) ) [18] => Array ( [id] => 17311 [weight] => 10000 [attrs] => Array ( ) ) [19] => Array ( [id] => 17511 [weight] => 10000 [attrs] => Array ( ) ) [20] => Array ( [id] => 18131 [weight] => 10000 [attrs] => Array ( ) ) [21] => Array ( [id] => 19171 [weight] => 10000 [attrs] => Array ( ) ) [22] => Array ( [id] => 21134 [weight] => 10000 [attrs] => Array ( ) ) [23] => Array ( [id] => 23007 [weight] => 10000 [attrs] => Array ( ) ) [24] => Array ( [id] => 23230 [weight] => 10000 [attrs] => Array ( ) ) [25] => Array ( [id] => 23460 [weight] => 10000 [attrs] => Array ( ) ) [26] => Array ( [id] => 23473 [weight] => 10000 [attrs] => Array ( ) ) [27] => Array ( [id] => 25609 [weight] => 10000 [attrs] => Array ( ) ) [28] => Array ( [id] => 28093 [weight] => 10000 [attrs] => Array ( ) ) [29] => Array ( [id] => 28175 [weight] => 10000 [attrs] => Array ( ) ) [30] => Array ( [id] => 30364 [weight] => 10000 [attrs] => Array ( ) ) [31] => Array ( [id] => 30506 [weight] => 10000 [attrs] => Array ( ) ) [32] => Array ( [id] => 31439 [weight] => 10000 [attrs] => Array ( ) ) [33] => Array ( [id] => 31668 [weight] => 10000 [attrs] => Array ( ) ) [34] => Array ( [id] => 32017 [weight] => 10000 [attrs] => Array ( ) ) [35] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( ) ) [36] => Array ( [id] => 5362 [weight] => 1 [attrs] => Array ( ) ) [37] => Array ( [id] => 9175 [weight] => 1 [attrs] => Array ( ) ) [38] => Array ( [id] => 13817 [weight] => 1 [attrs] => Array ( ) ) [39] => Array ( [id] => 14132 [weight] => 1 [attrs] => Array ( ) ) [40] => Array ( [id] => 14658 [weight] => 1 [attrs] => Array ( ) ) [41] => Array ( [id] => 15792 [weight] => 1 [attrs] => Array ( ) ) [42] => Array ( [id] => 16615 [weight] => 1 [attrs] => Array ( ) ) [43] => Array ( [id] => 18177 [weight] => 1 [attrs] => Array ( ) ) [44] => Array ( [id] => 18581 [weight] => 1 [attrs] => Array ( ) ) [45] => Array ( [id] => 18618 [weight] => 1 [attrs] => Array ( ) ) [46] => Array ( [id] => 18665 [weight] => 1 [attrs] => Array ( ) ) [47] => Array ( [id] => 18718 [weight] => 1 [attrs] => Array ( ) ) [48] => Array ( [id] => 19096 [weight] => 1 [attrs] => Array ( ) ) [49] => Array ( [id] => 19128 [weight] => 1 [attrs] => Array ( ) ) [50] => Array ( [id] => 19464 [weight] => 1 [attrs] => Array ( ) ) [51] => Array ( [id] => 20014 [weight] => 1 [attrs] => Array ( ) ) [52] => Array ( [id] => 20144 [weight] => 1 [attrs] => Array ( ) ) [53] => Array ( [id] => 20306 [weight] => 1 [attrs] => Array ( ) ) [54] => Array ( [id] => 22604 [weight] => 1 [attrs] => Array ( ) ) [55] => Array ( [id] => 23870 [weight] => 1 [attrs] => Array ( ) ) [56] => Array ( [id] => 23994 [weight] => 1 [attrs] => Array ( ) ) [57] => Array ( [id] => 24507 [weight] => 1 [attrs] => Array ( ) ) [58] => Array ( [id] => 24617 [weight] => 1 [attrs] => Array ( ) ) [59] => Array ( [id] => 25043 [weight] => 1 [attrs] => Array ( ) ) [60] => Array ( [id] => 25350 [weight] => 1 [attrs] => Array ( ) ) [61] => Array ( [id] => 26063 [weight] => 1 [attrs] => Array ( ) ) [62] => Array ( [id] => 26614 [weight] => 1 [attrs] => Array ( ) ) [63] => Array ( [id] => 26819 [weight] => 1 [attrs] => Array ( ) ) [64] => Array ( [id] => 28292 [weight] => 1 [attrs] => Array ( ) ) [65] => Array ( [id] => 29155 [weight] => 1 [attrs] => Array ( ) ) [66] => Array ( [id] => 30854 [weight] => 1 [attrs] => Array ( ) ) [67] => Array ( [id] => 31566 [weight] => 1 [attrs] => Array ( ) ) [68] => Array ( [id] => 31704 [weight] => 1 [attrs] => Array ( ) ) [69] => Array ( [id] => 32092 [weight] => 1 [attrs] => Array ( ) ) ) [total] => 70 [total_found] => 70 [time] => 0,000 [words] => Array ( [*test*] => Array ( [docs] => 70 [hits] => 70 ) ) )
print_r with "test" as a search word without setFieldWeights
Array ( [error] => [warning] => [status] => 0 [fields] => Array ( [0] => title [1] => full_story [2] => category [3] => date ) [attrs] => Array ( ) [matches] => Array ( [0] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( ) ) [1] => Array ( [id] => 1933 [weight] => 1 [attrs] => Array ( ) ) [2] => Array ( [id] => 2824 [weight] => 1 [attrs] => Array ( ) ) [3] => Array ( [id] => 2860 [weight] => 1 [attrs] => Array ( ) ) [4] => Array ( [id] => 2865 [weight] => 1 [attrs] => Array ( ) ) [5] => Array ( [id] => 5362 [weight] => 1 [attrs] => Array ( ) ) [6] => Array ( [id] => 5720 [weight] => 1 [attrs] => Array ( ) ) [7] => Array ( [id] => 6963 [weight] => 1 [attrs] => Array ( ) ) [8] => Array ( [id] => 8190 [weight] => 1 [attrs] => Array ( ) ) [9] => Array ( [id] => 8591 [weight] => 1 [attrs] => Array ( ) ) [10] => Array ( [id] => 9175 [weight] => 1 [attrs] => Array ( ) ) [11] => Array ( [id] => 10114 [weight] => 1 [attrs] => Array ( ) ) [12] => Array ( [id] => 10157 [weight] => 1 [attrs] => Array ( ) ) [13] => Array ( [id] => 11162 [weight] => 1 [attrs] => Array ( ) ) [14] => Array ( [id] => 11659 [weight] => 1 [attrs] => Array ( ) ) [15] => Array ( [id] => 13219 [weight] => 1 [attrs] => Array ( ) ) [16] => Array ( [id] => 13220 [weight] => 1 [attrs] => Array ( ) ) [17] => Array ( [id] => 13565 [weight] => 1 [attrs] => Array ( ) ) [18] => Array ( [id] => 13576 [weight] => 1 [attrs] => Array ( ) ) [19] => Array ( [id] => 13817 [weight] => 1 [attrs] => Array ( ) ) [20] => Array ( [id] => 14132 [weight] => 1 [attrs] => Array ( ) ) [21] => Array ( [id] => 14658 [weight] => 1 [attrs] => Array ( ) ) [22] => Array ( [id] => 14801 [weight] => 1 [attrs] => Array ( ) ) [23] => Array ( [id] => 14857 [weight] => 1 [attrs] => Array ( ) ) [24] => Array ( [id] => 15792 [weight] => 1 [attrs] => Array ( ) ) [25] => Array ( [id] => 16615 [weight] => 1 [attrs] => Array ( ) ) [26] => Array ( [id] => 17311 [weight] => 1 [attrs] => Array ( ) ) [27] => Array ( [id] => 17511 [weight] => 1 [attrs] => Array ( ) ) [28] => Array ( [id] => 18131 [weight] => 1 [attrs] => Array ( ) ) [29] => Array ( [id] => 18177 [weight] => 1 [attrs] => Array ( ) ) [30] => Array ( [id] => 18581 [weight] => 1 [attrs] => Array ( ) ) [31] => Array ( [id] => 18618 [weight] => 1 [attrs] => Array ( ) ) [32] => Array ( [id] => 18665 [weight] => 1 [attrs] => Array ( ) ) [33] => Array ( [id] => 18718 [weight] => 1 [attrs] => Array ( ) ) [34] => Array ( [id] => 19096 [weight] => 1 [attrs] => Array ( ) ) [35] => Array ( [id] => 19128 [weight] => 1 [attrs] => Array ( ) ) [36] => Array ( [id] => 19171 [weight] => 1 [attrs] => Array ( ) ) [37] => Array ( [id] => 19464 [weight] => 1 [attrs] => Array ( ) ) [38] => Array ( [id] => 20014 [weight] => 1 [attrs] => Array ( ) ) [39] => Array ( [id] => 20144 [weight] => 1 [attrs] => Array ( ) ) [40] => Array ( [id] => 20306 [weight] => 1 [attrs] => Array ( ) ) [41] => Array ( [id] => 21134 [weight] => 1 [attrs] => Array ( ) ) [42] => Array ( [id] => 22604 [weight] => 1 [attrs] => Array ( ) ) [43] => Array ( [id] => 23007 [weight] => 1 [attrs] => Array ( ) ) [44] => Array ( [id] => 23230 [weight] => 1 [attrs] => Array ( ) ) [45] => Array ( [id] => 23460 [weight] => 1 [attrs] => Array ( ) ) [46] => Array ( [id] => 23473 [weight] => 1 [attrs] => Array ( ) ) [47] => Array ( [id] => 23870 [weight] => 1 [attrs] => Array ( ) ) [48] => Array ( [id] => 23994 [weight] => 1 [attrs] => Array ( ) ) [49] => Array ( [id] => 24507 [weight] => 1 [attrs] => Array ( ) ) [50] => Array ( [id] => 24617 [weight] => 1 [attrs] => Array ( ) ) [51] => Array ( [id] => 25043 [weight] => 1 [attrs] => Array ( ) ) [52] => Array ( [id] => 25350 [weight] => 1 [attrs] => Array ( ) ) [53] => Array ( [id] => 25609 [weight] => 1 [attrs] => Array ( ) ) [54] => Array ( [id] => 26063 [weight] => 1 [attrs] => Array ( ) ) [55] => Array ( [id] => 26614 [weight] => 1 [attrs] => Array ( ) ) [56] => Array ( [id] => 26819 [weight] => 1 [attrs] => Array ( ) ) [57] => Array ( [id] => 28093 [weight] => 1 [attrs] => Array ( ) ) [58] => Array ( [id] => 28175 [weight] => 1 [attrs] => Array ( ) ) [59] => Array ( [id] => 28292 [weight] => 1 [attrs] => Array ( ) ) [60] => Array ( [id] => 29155 [weight] => 1 [attrs] => Array ( ) ) [61] => Array ( [id] => 30364 [weight] => 1 [attrs] => Array ( ) ) [62] => Array ( [id] => 30506 [weight] => 1 [attrs] => Array ( ) ) [63] => Array ( [id] => 30854 [weight] => 1 [attrs] => Array ( ) ) [64] => Array ( [id] => 31439 [weight] => 1 [attrs] => Array ( ) ) [65] => Array ( [id] => 31566 [weight] => 1 [attrs] => Array ( ) ) [66] => Array ( [id] => 31668 [weight] => 1 [attrs] => Array ( ) ) [67] => Array ( [id] => 31704 [weight] => 1 [attrs] => Array ( ) ) [68] => Array ( [id] => 32017 [weight] => 1 [attrs] => Array ( ) ) [69] => Array ( [id] => 32092 [weight] => 1 [attrs] => Array ( ) ) ) [total] => 70 [total_found] => 70 [time] => 0,000 [words] => Array ( [*test*] => Array ( [docs] => 70 [hits] => 70 ) ) )
sphinx php api
// Include the sphinx API class
require(ENGINE_DIR . "/modules/sphinxapi.php");
$sp = new SphinxClient();
$sp->setFieldWeights(array('title'=>10,'full_story'=>1));
$sp->SetMatchMode(SPH_MATCH_all);
$sp->SetArrayResult(true);
$sp->SetLimits(0,$count_result = ($config['search_number'] * 5));
$query = explode(" ",$story);
$query = implode(array_map(function($item) { return ' *' . $item . '* '; }, $query));
$results = $sp->Query($query, $config['sphinx_index']);
print_r($results);
$count_result = $results['total_found'];
$result_count = $count_result;
than i'm performing a select in on ids
$sql_request = "SELECT * from post WHERE id IN {$rIds} LIMIT $search_start,{$config['search_number']}";
sphinx conf
type = mysql
sql_host = localhost
sql_user = user
sql_pass = pass
sql_db = user
sql_port = 3306 # optional, default is 3306
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM post
sql_query_pre = SET NAMES utf8
sql_query_pre = SET CHARACTER SET utf8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query = SELECT id, title, full_story, category, DATE FROM post WHERE approve='1' AND id <=( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 )
#sql_attr_timestamp = DATE
sql_ranged_throttle = 0
index
index online_test
{
source = online_test_posts
path = /var/lib/sphinx/online_test
docinfo = extern
charset_type = utf-8
morphology = stem_enru
min_word_len = 1
min_prefix_len = 0
min_infix_len = 1
mlock = 0
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
enable_star = 1
rt_mem_limit = 512M
}
my indication is a post with a title "Test Your Brain" with an id '2865' it's should be at least second. instead i'm getting post with a title "Coming & Going" with full_story content test, id post - 1432
by the way, if I'm running search from command line the results are much more accurate.
search test
index 'online_test': query 'test ': returned 7 matches of 7 total in 0.008 sec
displaying matches:
1. document=2865, weight=1684
2. document=5362, weight=1684
3. document=8190, weight=1684
4. document=23460, weight=1684
5. document=25609, weight=1684
6. document=31704, weight=1684
7. document=32092, weight=1684
Well you've proved yourself, that setFieldWeights is working :)
When no weights: [4] => Array ( [id] => 2865 [weight] => 1 [attrs] => Array ( )
When 10: [4] => Array ( [id] => 5720 [weight] => 10 [attrs] => Array ( [date] => 2013 )
When 10000: [4] => Array ( [id] => 5720 [weight] => 10000 [attrs] => Array ( )
And when have weights set, you have documents with different weights:
[34] => Array ( [id] => 32017 [weight] => 10 [attrs] => Array ( [date] => 2013 )
[35] => Array ( [id] => 1432 [weight] => 1 [attrs] => Array ( [date] => 2012 )
... so some results (persuambly with a match on the title) have a higher wieght, and promoted above the description matches.
The fact that some tests have the date atttribute, and some dont, is just because you run the tests at different times? (so not a real problem, just an artifact of your testing?)
What I wonder, if you realise, you are just doing:
$sql_request = "SELECT * from post WHERE id IN {$rIds} LIMIT $search_start,{$config['search_number']}";
which will just return the results in arbitary order (what ever mysql deems fit).
Maybe you are looking at the results of the mysql query directly, and not realising you need to sort the results yourself, so they match the order returned from sphinx.
And you have odd LIMIT. The SQL query shouldnt have a limit, because paging is done in sphinx NOT in mysql.
Suggest
$sql_request = "SELECT * from post WHERE id IN {$rIds} ORDER BY FIELD(id,{$rIds})";
So that mysql will return you the results sorted correctly.
From: http://sphinxsearch.com/info/faq/
I have an array that I need to process by adding the values, then removing any added duplicates.
Here's the Original Array...
Array (
[0] => Array ( [PID] => 2872 [QTY] => 1 )
[1] => Array ( [PID] => 3145 [QTY] => 2 )
[2] => Array ( [PID] => 3107 [QTY] => 1 )
[3] => Array ( [PID] => 2739 [QTY] => 1 )
[4] => Array ( [PID] => 3137 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 1 )
[6] => Array ( [PID] => 2739 [QTY] => 1 )
[7] => Array ( [PID] => 3107 [QTY] => 1 )
[8] => Array ( [PID] => 3137 [QTY] => 4 )
[9] => Array ( [PID] => 3551 [QTY] => 1 )
[10] => Array ( [PID] => 3107 [QTY] => 1 )
[11] => Array ( [PID] => 3107 [QTY] => 1 )
[12] => Array ( [PID] => 3137 [QTY] => 1 )
[13] => Array ( [PID] => 3551 [QTY] => 2 )
[14] => Array ( [PID] => 3136 [QTY] => 1 )
[15] => Array ( [PID] => 3137 [QTY] => 1 )
[16] => Array ( [PID] => 3032 [QTY] => 1 )
[17] => Array ( [PID] => 3551 [QTY] => 1 )
[18] => Array ( [PID] => 3107 [QTY] => 1 )
[19] => Array ( [PID] => 3459 [QTY] => 1 )
[20] => Array ( [PID] => 3141 [QTY] => 1 )
[21] => Array ( [PID] => 2724 [QTY] => 1 )
[22] => Array ( [PID] => 2743 [QTY] => 1 )
[23] => Array ( [PID] => 3139 [QTY] => 2 )
[24] => Array ( [PID] => 3137 [QTY] => 2 )
[25] => Array ( [PID] => 3107 [QTY] => 1 )
)
What I need to do is take this array and Add the [QTY] values from the same [PID] values then after this end up with an array like this...
Array (
[0] => Array ( [PID] => 2724 [QTY] => 1 )
[1] => Array ( [PID] => 2739 [QTY] => 2 )
[2] => Array ( [PID] => 2743 [QTY] => 1 )
[3] => Array ( [PID] => 2872 [QTY] => 1 )
[4] => Array ( [PID] => 3032 [QTY] => 1 )
[5] => Array ( [PID] => 3107 [QTY] => 7 )
[6] => Array ( [PID] => 3136 [QTY] => 1 )
[7] => Array ( [PID] => 3137 [QTY] => 9 )
[8] => Array ( [PID] => 3139 [QTY] => 2 )
[9] => Array ( [PID] => 3141 [QTY] => 1 )
[10] => Array ( [PID] => 3145 [QTY] => 2 )
[11] => Array ( [PID] => 3459 [QTY] => 1 )
[12] => Array ( [PID] => 3551 [QTY] => 4 )
)
So add all the QTY then remove the duplicates.
I'm not quite sure the best way to proceed here. Any suggestions?
Loop over them, make a new array with PID as key and QTY as value so you can add the latter up.
foreach ($array as $row) {
list($pid, $qty) = $row;
$sums[$pid] += $qty;
}
Obviously you could throw an isset in to suppress the notices.
And if you want your former array structure, convert it once more.
Id just use a simple loop:
$combined = array();
// $org will be the original structure
foreach($org as $id => $data) {
$pid = $data['PID'];
if(!isset($combined[$pid])) {
$combined[$pid] = $data;
} else {
$combined[$pid]['QTY'] += $data['QTY'];
}
}
// return keys to normal indexs instead of the PID
array_values($combined);
Ok so here is my first array:
(
[1] => Array
(
[27] => Array
(
[product_id] => 27
[type] => hardware
[step_number] => 1
)
[372] => Array
(
[product_id] => 372
[type] => hardware
[step_number] => 1
)
[92] => Array
(
[product_id] => 92
[type] => hardware
[step_number] => 1
)
)
[2] => Array
(
[335] => Array
(
[product_id] => 335
[type] => hardware
[step_number] => 2
)
[62] => Array
(
[product_id] => 62
[type] => hardware
[step_number] => 2
)
[356] => Array
(
[product_id] => 356
[type] => hardware
[step_number] => 2
)
)
and here is my second array
(
[1] => Array
(
[655] => Array
(
[product_id] => 655
[type] => optional
[step_number] => 1
)
[54] => Array
(
[product_id] => 54
[type] => optional
[step_number] => 1
)
[554] => Array
(
[product_id] => 554
[type] => optional
[step_number] => 1
)
)
[2] => Array
(
[33] => Array
(
[product_id] => 33
[type] => optional
[step_number] => 2
)
[612] => Array
(
[product_id] => 612
[type] => optional
[step_number] => 2
)
[5] => Array
(
[product_id] => 5
[type] => optional
[step_number] => 2
)
)
[3] => Array
(
[444] => Array
(
[product_id] => 444
[type] => optional
[step_number] => 3
)
[6] => Array
(
[product_id] => 6
[type] => optional
[step_number] => 3
)
[53] => Array
(
[product_id] => 53
[type] => optional
[step_number] => 3
)
)
Basically what i need is the second array appended to the end of the first array with the keys of the first array preserved the keys and changing the step_number to the new key so the final keys looks like
(
[1] => Array
(
[27] => Array
(
[step_number] => 1)
....
[2] => Array
(
[335] => Array
(
[step_number] => 2)
....
[3] => Array
(
[655] => Array
(
[step_number] => 3)
....
[4] => Array
(
[33] => Array
(
[step_number] => 4)
....
[5] => Array
(
[444] => Array
(
[step_number] => 5)
....
but when i do
$all = array_merge($first, $second);
the keys are
(
[0] => Array
[1] => Array
[2] => Array
[3] => Array
[4] => Array
Is that possible to do....
Try array_merge_recursive .. http://php.net/array_merge_recursive
This is also from the manual on array_splice if you're just looking to append your 2nd array onto the first (http://php.net/manual/en/function.array-push.php):
array_splice($first, count($first), 0, $second);
Iterating over the first set of keys, then using the Union array operator should do the trick.
//$first = array(...);
//$second = array(...);
foreach ($second as $key => $value)
{
if (!isset($first[$key]))
{
$first[$key] = array();
}
$first[$key] += $value;
}