I'm trying to convert my multidimensional array to string in order to write it to file.
Scenario: Gather data in array, convert it to formatted string for better readability and write it to file.
Issue: When I try to convert multidimensional array such as below with netsted foreach, it doesn't work as intended; mostly I get partial data with Array outputs or just Array outputs. Also when I try implode() function, it only fetches the deepest values.
Sample multidimensional array output;
Array
(
[09_ERROR_TEXT_DESCR] => Array
(
[LINES] => Array
(
[0] => 1732
[1] => 1858
)
[COUNT] => 2
)
[12_ERROR_TEXT_DESCR] => Array
(
[LINES] => Array
(
[0] => 1936
)
[COUNT] => 1
)
[14_ERROR_TEXT_DESCR] => Array
(
[LINES] => Array
(
[0] => 1365
[1] => 1476
[2] => 1697
)
[COUNT] => 3
)
[15_ERROR_TEXT_DESCR] => Array
(
[LINES] => Array
(
[0] => 1697
)
[COUNT] => 1
)
)
Desired string output to write it to file;
09_ERROR_TEXT_DESCR|1732,1858|2
12_ERROR_TEXT_DESCR|1936|1
14_ERROR_TEXT_DESCR|1365,1476,1697|3
15_ERROR_TEXT_DESCR|1687|1
Note:
I use file_put_contents($debug_log_path, $result_set, LOCK_EX); to write file since on each run, I want file to be overwritten with new data. That's why I want to convert it into string.
(,)the inner dimension first (lines), then declare them inside another array and implode again (|) for the final row along with the key. just like the answer below