Pelin
Pelin

Reputation: 457

Multidimensional array to formatted string

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.

Upvotes: 0

Views: 87

Answers (2)

XedinUnknown
XedinUnknown

Reputation: 712

You can probably do it much easier by using var_export() or json_encode(). This will produce a string similar to the following, which you can then write to file:

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,
  ),
)

See example.

Upvotes: 0

Nick
Nick

Reputation: 147216

You can achieve what you want with a foreach over the array, extracting the key, LINES and COUNT values and imploding them to a string. I've demonstrated appending each string to an array, you can either write to the file as you go or write the whole array to a file in one go:

$result_set = [];
foreach ($array as $key => $value) {
    $result_set[] = implode('|', array($key, implode(',', $value['LINES']), $value['COUNT']));
}
print_r($result_set);

Output:

Array
(
    [0] => 09_ERROR_TEXT_DESCR|1732,1858|2
    [1] => 12_ERROR_TEXT_DESCR|1936|1
    [2] => 14_ERROR_TEXT_DESCR|1365,1476,1697|3
    [3] => 15_ERROR_TEXT_DESCR|1697|1
)

Demo on 3v4l.org

Upvotes: 1

Related Questions