Navigating an array in PHP using a loop

Is it possible to reference a value of an array within another array?

For instance.

In this php code snippet, I am looping through an array as shown below. Is there a way for me to easily retrieve the [product_name] => DIESEL #2 data without using another loop? Assuming that the data will always be structured in this manner. Maybe something like $

fuel = $value['transaction_id'.'product'.'product_name']
?

foreach ($result['data'] as $key => $value) {
    $id =  $value['id'];
    $transactionId =  "FC".$value['transaction_id'];
    $transactionDateTime = $value['created_utc'];
    echo $id." - ".$transactionDateTime." - ".$transactionId." <br>";
}



(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 5750574
                    [label] => 
                    [self] => https://api.fuelcloud.com/rest/v1.0/transaction/5750574
                    [transaction_id] => 0025621e-3270-c0e2-8d2a-6425ed834f9d
                    [transaction_type] => 1
                    [tank_id] => 23744
                    [start_date] => 2019-12-19T00:00:00
                    [end_date] => 2019-12-19T23:59:59Z
                    [site_id] => 23743
                    [product_id] => 409
                    [product] => Array
                        (
                            [id] => 409
                            [product_name] => DIESEL #2
                            [product_category] => diesel
                        )

Answer №1

It appears that you are already familiar with how to reference array entries, such as using $value['id']. Another array within this context is $value['product'], and

$value['product']['product_name']
represents the value associated with the key product_name from that particular array.

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

In the console, a JSON string is displayed, however the PHP variable outputs as null

Below is the snippet of javascript code I am working with: $('.showEditForm').click(function () { var webpagefileID = this.id; if($('#editForm').css('display', 'none')) { $('#editForm').css ...

Incorporate jQuery into Laravel 5.4 mix for enhanced functionality

After being familiar with Laravel 5.1 mix (elixir), I decided to give Laravel 5.4 mix a try. I combined my libraries into vendor files. mix .styles([ './node_modules/bootstrap/dist/css/bootstrap.css', './node_modules/font-awesome/c ...

Adding several entries into mysql with the assistance of php

I've been attempting to input multiple records into my database table, but every time I try, all I see are zeros(0) inserted into the fields. Here's what I attempted: I used a foreach loop for insertion, but unfortunately it doesn't seem to ...

Discover the best way to pass multiple input data using Ajax and jQuery

I'm encountering an issue while attempting to pass multiple input values through Ajax with jQuery, as it seems to not be functioning correctly. For instance: <input type="text" class="form-control invoice_item_name" name=" ...

What is the proper way to raise an Exception and then handle it in code?

Is there a way to store an error message in my $message variable if an exception is thrown from the model? I have managed to pass the exception to the controller, but I am unsure of how to handle it in the view. Below is an example illustrating what I am ...

Inquire anonymously in DWQA (a WordPress plugin) without the need to log in or provide an email

Is it possible to add questions in the QW Question & Answer Wordpress plugin without requiring users to register or provide their email address? I am looking for a way for all users, even guests, to ask questions without the need to enter their email addr ...

Discover the seamless method of transferring data from an HTML5 form to a PHP page for processing without the need to refresh the page or manually navigate to the PHP file

When creating a website, I encountered an issue with a form that requires the user to select three options from select elements and then click a search button to search the database. The problem is that PHP does not retrieve the form data unless the button ...

Guide on integrating buefy (a vue.js component library) into your Laravel blade template

I'm currently integrating buefy into my project, but I'm encountering issues with using vue.js on Laravel 5.8. Can anyone offer assistance? Here is the code snippet from my app.js: require('./bootstrap'); window.Vue = require('v ...

Issues with Codeigniter Ajax Post functionality

Hey everyone, I'm working on implementing a simple voting system for comments using jQuery Ajax to avoid page refreshing when someone likes or dislikes a comment. Here is the jQuery code I have so far: $(document).ready(function(){ $(".vote-btn" ...

Managing several versions within a single URL

My goal is to create a system that allows for multiple versions of the same web application, similar to how Google offers new versions of their products with a "Try the new version" link. The idea is to have both a "stable" and a "beta" version of the web ...

Splitting a row within a column using an SQL query

There is a row containing: (apple, banana, cherry, grape) AND another row with: (1, 2, 3, 4, 5) I want to display this data in two columns, matching each fruit with its corresponding number like so: FRUITS | NUMBERS -------------------- apple 1 ...

Develop an XML document with the use of either Javascript or PHP

I have an XML file that contains information about bracelets with photo details. <bracelets> <photo filename="b1.jpg" thumbnail="a1.jpg" description="aa" /> <photo filename="b2.jpg" thumbnail="a2.jpg" description="aa" /> & ...

Unexpected server error encountered during navigation of php pages

I am using PHP to retrieve data from a MySQL table and display it in an HTML table. Each row in the table has a 'remove' button that triggers a PHP script (remove.php) to delete the corresponding row from the database table and return to admin.ph ...

Should I implement this practice when developing an AJAX website? Is it recommended to enable PHP code within .html files, or should I switch to using .php files instead?

Query: I am interested in executing PHP within HTML documents to include HTML using PHP include();. Question: Would it be more beneficial to change .php to .txt for my AJAX-loaded pages and switch my .html files to .php? This approach might resolve the ...

Invalid index when trying to add to an array

I am currently faced with a challenge in modifying an array that I have at hand; +rows: array:31 [▼ 0 => array:2 [▼ 0 => "20190101" 1 => "5" ] 1 => array:2 [▼ 0 => "20190102" 1 => "15" ] ...

Forward user to a new page following successful login utilizing angular.js router and PHP

I am in need of assistance. I am looking to implement a page redirection after a user logs in using Angular.js with PHP as the backend. I have written some code, but it seems to be not functioning correctly. Below is an explanation of my code. index.htm ...

Combine and update JSON files by matching ID's in PHP

I am facing an issue with merging JSON files in PHP. I want to overwrite data when the id's match, but it's not working as expected. Here is my PHP code: foreach($instagram_array['data'] as $key => $image){ $id = $image['id ...

Having trouble with the mysqli_query function, it's not working as expected. Can someone offer assistance?

Could use some help with my mysqli_query. It's not inserting into the database, even though it works in phpmyadmin SQL but not on the website. Here is my code: <form action="" method="POST> <input type="email" name="email" placeholder="P ...

Using UTF-8 encoding for Hebrew characters and large question marks

I have gone through numerous articles, but the concept is still not clear to me. I am currently retrieving text from a file using the following code snippet: $fp = fopen($storagename, 'r'); while ( !feof($fp) ){ $line = fgets($fp, 2048); ...

PHP's shell_exec function does not support executing the command "Watir::Browser.new :firefox"

I've recently started learning about ruby and Watir, and I'm facing a problem... I have a MySQL database containing test data that I need to access. Over time, I have built up a collection of PHP tools for interacting with this data, such as mar ...