'PHP Array echo 1 by 1
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] =>
)
Solution 1:[1]
I made some test if the key value exist using isset. If you wanted to create a table, you needed to use <table></table> tags.
<?php
$arrayfinancialentry1['financial_entry'][] = array('financial_date' => '31/01/2022 18:12:00', 'account' => '333515', 'amount' => '100', 'reference' => '220131181159903');
$arrayfinancialentry1['financial_entry'][] = array('financial_date' => '31/01/2022 16:55:26', 'account' => '333515', 'amount' => '100', 'reference' => '220131165525613');
$arrayfinancialentry1['financial_entry'][] = array('financial_date' => '31/01/2022 12:38:15', 'account' => '333515', 'amount' => '100', 'reference' => '10031123815407');
?>
<table>
<tbody>
<?php
if (isset($arrayfinancialentry1['financial_entry'])) {
foreach ($arrayfinancialentry1['financial_entry'] as $result) {
echo "<tr>";
if (isset($result['reference'])) {
echo "<td>{$result['reference']} </td>";
} else {
echo "<td></td>";
}
if (isset($result['financial_date'])) {
echo "<td>{$result['financial_date']} </td>";
} else {
echo "<td></td>";
}
if (isset($result['account'])) {
echo "<td>{$result['account']} </td>";
} else {
echo "<td></td>";
}
if (isset($result['amount'])) {
echo "<td>{$result['amount']} </td>";
} else {
echo "<td></td>";
}
echo "</tr>";
}
}
?>
</tbody>
</table>
You can run the code here : http://sandbox.onlinephpfunctions.com/code/21f00292848b2e367ccf0113c76731d18e8f7f82
Solution 2:[2]
Try this
Your $arrayfinancialentry1 is an array with a key [financial_entry] and this key has other arrays as value,
So when you do $arrayfinancialentry1 as $result you key [financial_entry] is now $result array which doesn't have columns your looking for but arrays inside it has those.So follow the below code.
if(!empty($arrayfinancialentry1)){
foreach ($arrayfinancialentry1 as $key => $result) {
echo "<tr>
<td>{$result['reference']} </td>
<td>{$result['financial_date']}</td>
<td>{$result['account']}</td>
<td>{$result['amount']}</td>
</tr>";
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | executable |
| Solution 2 |
