John Davidson

PHP Array echo 1 by 1

0 comments
Message:


I am writting simple table output using PHP and the response of the API is Array.


Array
(
[financial_entry] => Array
(
[0] => Array
(
[financial_date] => 31/01/2022 18:12:00
[account] => 333515
[amount] => 100
[reference] => 220131181159903
)

[1] => Array
(
[financial_date] => 31/01/2022 16:55:26
[account] => 333515
[amount] => 100
[reference] => 220131165525613
)

[2] => Array
(
[financial_date] => 31/01/2022 12:38:15
[account] => 333515
[amount] => 100
[reference] => 10031123815407
)

)

)

I want to create a table that I can echo the data one by one. My desired output is like this the screenshot ->https://prnt.sc/26nvu4m


I tried using the below code but error -



Warning: Illegal string offset 'amount' in



foreach ($arrayfinancialentry1 as $result) {

echo "<tr>
<td>{$result['reference']} </td>
<td>{$result['financial_date']}</td>
<td>{$result['account']}</td>
<td>{$result['amount']}</td>
</tr>";
}


Note: Sometimes the response of the Array is only because the data is empty.


Array
(
[0] =>

)


My Answer:
> Here is an example of how you can echo each element of the array one by one in a table format:

php
// Sample array from API response
$array = [
'financial_entry' => 'Value 1',
'another_entry' => 'Value 2',
'yet_another_entry' => 'Value 3'
];

echo '';
foreach ($array as $key => $value) {
echo '';
echo '';
echo '';
echo '';
}
echo '
' . $key . '' . $value . '
';
?>


This code will output a simple table with two columns, one for the array key and one for the array value. You can modify the code to fit your specific array structure and styling needs.

Rate this post

5 of 5 based on 3022 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us