I am looking to get the lowest price from some json data, I get the info from an api call, and json_decode it.
The format is:
products
productnumber -> price
$product_array['products'][..changingnumber..]['value']
I dont know the productnumber (and don't need it either). I do want find the lowestprice for all productnumbers combined
$price = min($product_array['products'][..changingnumber..]['value']);
Is there a way to do this? or is there some documentation that can help with this?
First map the 'value'-s, transform them into a simple array containing all the prices:
$all_prices = array_map(function($item) {
return $item['value'];
}, $product_array['products'])
Then just choose the minimum:
$price = min($all_prices);
Or combined into a single statement:
$price = min(array_map(function($item) {
return $item['value'];
}, $product_array['products']));
Related
I want to get data that Price is greater than or equal to a specified value (taken from the input).
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$value = Dproduct::where(['status' => 1])->where('price', '>', $minPrice)->get();
But the Price is JSON in database, I can't get it. Currencies such as: VND, USD, TWD,...
All answers are respected!
Thanks so much!
if you are using laravel 5.7 or greater then you may be able to select within the JSON. See the docs here for more details.
Note: for this to work you must use json data type for your field in database.
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$value = Dproduct::where(['status' => 1])->where('price->USD', $minPrice)->get();
I am sorry if my title is incorrect, was not sure what these array values are called.
I am doing an api call that returns data, i want to use that data.
Now in the data it is possible to have up to 20 'offers'.
Each has their own price.
I would like to return the lowest price
The structure of the results are
results -> 0 -> offers -> "number from 0-19" -> price
So each offer (with number 0-19) will have a price.
Is there an easy way to grab all of that data at once and just output the lowest price?
$price = $price_array['results'][0]['offers']['*can i cycle this part*']['price'];
foreach ($price_array['results'][0]['offers'] as $offer) {
echo $offer['price'];
// and do what you want
}
Or maybe:
$minPrice = min(array_column($price_array['results'][0]['offers'], 'price'));
echo $minPrice;
You can use array_column and min().
$price = min(array_column($price_array['results'][0]['offers'],'price'));
This will return the lowest price in that column of the array.
I am constructing a function that is making a call with API to db and returns me JSON data. The json contains orders and I have already decoded them in php array. Every order has properties such as "product_id" and "quantity". I want to temporarily store the "quantity" property somewhere because I need to sum all the products with the same product_id. Any suggestion how to do this?
I'm in a bit of a hurry, but wanted to see if I could help you out.
$quantities = [];
//Loop through all the orders
foreach ($orders as $order) {
//Loop through the orderrows
foreach ($order->getOrderRows() as $orderRow) {
if (array_key_exists($orderRow->getProductName(), $quantities)) {
//The product is already in the quantities array, so the quantity of this orderrow is added to the total
$quantities[$orderRow->getProductName()] =
($quantities[$orderRow->getProductName()] + (int) $orderRow->getItemQuantity());
} else {
//If this is the first time the product is encountered add the product and its quantity to the quantities array
$quantities[$orderRow->getProductName()] = (int) $orderRow->getItemQuantity();
}
}
}
This is going to have the following result, showing you the product names along with their quantities:
$quantities = [
'foo' => 12,
'bar' => 3,
];
You may use the session variable to store these values.
I am creating custom emails to be sent to customers wanting to share their cart with others. So far, I have each product's quantity, SKU, price, path (node/XYZ), and title. The last item I need in the email is the product's image path.
I found all the other information with the following:
$order = commerce_cart_order_load($user->uid);
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
$sku = $line_item_wrapper->line_item_label->value();
//...
Printing out the following I was able to see a protected "data" property for the wrapper object:
print_r($line_item_wrapper->commerce_product);
Then, I tried finding the getter method for the field_image property with the following:
print_r($line_item_wrapper->commerce_product->getPropertyInfo('field_image');
I ended up here with entity_metadata_field_verbatim_get() but I don't know what parameters to pass. Also, in the last print statement above I didn't see anything else of value.
I'm wondering if I need to query for this data, and what table / columns to query for? Or maybe use something like node_load()? However, i'm not finding it too easy to find the node ID from the line item wrapper.
I was able to solve the above problem with this code:
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$result = array();
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
$product = array();
$product['quantity'] = $line_item_wrapper->quantity->value();
$product['sku'] = $line_item_wrapper->line_item_label->value();
$product['path'] = $line_item_wrapper->commerce_display_path->value();
$product['price'] = $line_item_wrapper->commerce_unit_price->amount->value();
$product['title'] = $line_item_wrapper->commerce_product->title->value();
$product['image'] = $line_item_wrapper->commerce_product->field_image->value();
$product['image'] = $product['image'][0]['filename'];
array_push($result, $product);
}
I have to say, finding all these discrete functions was quite difficult. I did not find any clear documentation about Drupal Commerce metadata_wrappers. If anyone could provide further information I'd love to take a look.
In general, I used a lot of print_r(); to find these values.
I have list of items that need to be processed and printed to the user.
Each item on the list needs to have a unique set of calculations to it.
The only thing is, the user also supplies me with the order of how items will be printed & handled first - for example: item4,item2,item1,item3
How can one approach this in PHP?
I thought of running a for loop against the user submitted ordered list and tackle each calculation and print it to the user, the problem is that this will be hard to maintain because the user will have to edit the for loop each time he will want to add a new item or calculation.
Given a list of 1-n items:
$items['item1'] = $item1;
With a co-related list of 1-n functions:
$functions['item1'] = function($item) {return ...;};
You can sort items according to the user-input:
$subset = orderAndFilterItems($items, $userInput);
and then iterate:
foreach($subset as $key => $item)
{
$function = $functions[$key];
$value = $function($item);
}
Naturally you can encapsulate this further on, but it should give you the idea.