0

Is there a way to refer to a value of an array inside of an aray?

For example.

I have the following php code to loop through an array listed below. Can I easily get the [product_name] => DIESEL #2 data without another for loop? If I know the data will always come this way? 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
                        )
1
  • $fuel = $value['product']['product_name']; is it what you are loooking for? Commented Dec 20, 2019 at 16:43

1 Answer 1

1

You already know how to reference array entries, it seems, like $value['id']. $value['product'] is another array, and $value['product']['product_name'] is the value of the product_name key from that array.

Sign up to request clarification or add additional context in comments.

1 Comment

I feel so stupid. TY. I don't code in PHP full time. Looks like a simple syntax issue on my part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.