0

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.

6
  • 1
    what is the origin of array, and more importantly, where are the codes? Commented Sep 1, 2020 at 9:14
  • @Kevin I populate this array after checking inputs from several source codes fetched from my private GIT or databases with specific format. The codes I wrote with possibilities I mentioned above didn't work as intended and most was nonsense due to desperation I had. Commented Sep 1, 2020 at 9:17
  • 1
    basically, you just need to iterate on each batch, and inside, you'll need to implode (,) 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 Commented Sep 1, 2020 at 9:48
  • Thank you for explanation @Kevin, as Nick stated below what you've mentioned is working just as expected. Now I'm trying to figure out how I could messed up on such easy task :) Commented Sep 1, 2020 at 9:54
  • @Nick, I must have missed marking your answer which I thought I did. Your answer is an exact solution to my question. Commented Sep 5, 2020 at 10:32

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Thank you Xedin, I believe @Nick's solution is optimal
Depends. Using PHP native functions means no writing, maintaining, or debugging your own code, including code that will convert the output back to a PHP structure.. Using var_export() means that you can directly execute the string result as PHP. Using json_encode() means converting your data into a very common data interchange format, easily parsable and human-readable. What's optimal about having to write your own custom implementations for things that already exist and do things better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.