Skip to content
Advertisement

How to get the first element result in Php using foreach?

I tried to have this multi-dimensional array on Quotes>Sections>Departments I have a problem with getting the first iteration of the foreach-ed

$Department->getActuals();

$actuals = array();

foreach($quotes as $quoteID => $Quotes) {
                $invoiceTotal = $Quotes->getInvoicedValue();
                if($invoiceTotal['Total'] > 0) {
                    $labourCost = 0;
                    $actualHours = 0;
                    $sections = $Quotes->getSections();
                    foreach ($sections as $Sections) {
                        $departments = $Sections->getDepartments();
                        foreach($departments as $Department) {
                            $actuals = $Department->getActuals(null, null, false);
                            Console::Log('actuals', $actuals);
                        }
                    }
                }
            }

Console::Log(‘actuals’, $actuals);

How to get the first element result in Php using foreach?

Thank you in advance

Advertisement

Answer

If $actuals is an array, you can access the first element via array notation. Arrays in php are 0 based.

Console::Log('actuals', $actuals[0]);

Based on your statement that $actuals is an array with a number of associative keys within, you can get a human readable form using print_r, and passing the true parameter to return the output rather than printing it.

Console::Log('actuals', print_r($actuals, true));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement