Find the most affordable rate in the JSON data without knowledge of the parent label

I am seeking to extract the best deal from JSON data obtained through an API call and decoded using json_decode.

The structure is as follows: products productnumber -> price

$product_array['products'][..changingnumber..]['value']

Without knowing the product number (and not needing it), I aim to identify the lowest price among all combined product numbers.

$price = min($product_array['products'][..changingnumber..]['value']);

Is there a method to accomplish this task? Alternatively, are there any resources or documentation available to assist with this?

Answer №1

To start, map out the 'value'-s and convert them into a straightforward array that includes all the prices:

$all_prices = array_map(function($item) {
    return $item['value'];
}, $product_array['products'])

Next, simply select the minimum value:

$price = min($all_prices);

You can also combine these steps into one statement:

$price = min(array_map(function($item) {
    return $item['value'];
}, $product_array['products']));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Locate using the category title or create an additional table

My database contains a list of website addresses, each categorized with plain English categories such as movies, tutorials, etc. The table includes fields for site_id, site_url, and site_category. I am debating between keeping it in one table or splitting ...

Nested looping within a table structure

I had a Users table and a group table. When I load the group table, it loads the users which have the same group_id as the group table id. It works, but my issue is that the foreach loop was quite messy. The output ends up looking like this... However, I ...

"Optimizing website performance with Ajax and securing data with

As I work on developing an Ajax chat application, I have found the need to periodically call a PHP script that checks for new messages in the database. I recently learned about PDO prepared statements and thought they could be useful since only one variab ...

Guide on obtaining JSON data in an android application through a PHP file?

I am a beginner in Android development and I have recently created a simple data fetching application. However, I am facing an issue where I am only able to retrieve the value of one variable from my PHP file, even though both variables are present in the ...

Using Ruby to convert JSON data into an array

This is an example of JSON code containing job and applicant information { "jobs": [ { "id": 1, "title": "Software Developer", "applicants": [ { "id": 1, "name": "Rich Hickey", "tags": ["clojure", "java", "immutability", "datom ...

What are some best practices for utilizing the __dir__ feature?

I am trying to utilize the __dir__ feature. Unfortunately, I am having trouble finding a comprehensive guide on how to properly set it up. My htdocs are stored in Dropbox. Is the setup similar to this example? define(__DIR__, 'd:documents/dropbox/ ...

Converting Laravel Custom Error Validation JSON Response to an Array

I am currently working on developing an API for a registration form and running into an issue with the error format. While the validator correctly displays errors in an object format, I require the JSON response in an array format. $validator = Validato ...

What steps are involved in executing a standalone PHP script in Laravel?

Hello there! I am relatively new to the world of Laravel and PHP, so please bear with me if my query has been addressed previously. I have this standalone PHP script sitting pretty in my public folder: define('DB_HOST','localhost'); def ...

Utilizing Ajax: Sending Customized Data to a Modal

Having never worked with jquery before, I struggled to find a solution for my specific case. On the cockpit.php page, I have a form that retrieves content from a mysql database. Currently, I am able to display this content in a div on the same cockpit.php ...

The XML information vanished during the transformation into JSON format

After converting XML to JSON using multiple conversion libraries, I noticed that the property name attributes and Item name attributes were lost. Why is this happening? Does anyone have suggestions on how I can modify my XML to make it more compatible for ...

Is there a way to display a previous submission's value in a form field?

I have set up a form using RSForm with two textboxes. The first textbox is named km1 (new_km) and the second one is named km2 (old_km). Initially, the user will input their car's kilometer number in the km1 field (new_km). When the user revisits and ...

Generating and consuming XML data with swagger-node

As a newcomer to swagger-node (swagger-spec 2.0), I am looking to have my API consume and produce both XML and JSON as requested by the customer. Currently, I have only focused on the "producing" aspect of it. When it comes to producing a response, I am a ...

There was a problem encountered while trying to import Json data

I'm having trouble importing my JSON file into R. I've installed the necessary packages and here's what I attempted: library(rjson) JsonData <- fromJson(file= "<filename.json>") I also tried using the file path and the jsonlite pa ...

The method of altering a menu link in WordPress using jQuery varies according to whether the user is logged in or not

I need to update the last link on my menu. When a user is logged in, it should display a profile link; otherwise, it should show a sign-up link. ...

Get a URL from the JSON data returned by the Wikipedia API

How can I retrieve the image URL from a JSON response and store it in a variable? I found helpful information on the MediaWiki API help page Following this example to extract image information from a page: https://commons.wikimedia.org/w/api.php?action= ...

"The issue with the jQuery pop-up box overlay persisting and not closing

I have implemented a jQuery popup plugin on my WordPress site, and while the popup functions correctly, I am facing an issue with the styling. The design does not include an overlay to grey out the background, so I attempted to add classes and styles to th ...

A step-by-step guide to computing cumulative sum in an Array using PHP

Here's an example of an array: $numbers = array(23, 18, 5, 8, 10, 16); What I would like to achieve is a cumulative sum: 23, 41, 46, 54, 64, 80 To provide some context, this array is being used to generate a line graph. My objective is to include ...

Reading and processing Json data with a Rest Client and Object Mapper

Currently, I am working on a REST API using Java 17 with Spring Boot 3.2.2. This API interacts with an external REST API that returns JSON data in a non-traditional format. Instead of returning a JSON object, it provides the response as a JSON string. For ...

Issue with a PCRE regular expression

I'm currently working on a regular expression that needs to match the following pattern: argument ::= define_scope [';' define_scope]* define_scope ::= (['local'] | 'global') define_var define_var ::= variable_name expre ...

Having trouble accessing $this in a function within a view file in CodeIgniter?

After finding that one of my views in CodeIgniter had become too large, I decided to move some of the code into a function within the same file: function html_stuff() { $posts = $this->db->query('select * from posts'); } However, when ...