0

Dear all.
how can I put my output array into file exactly as it appear below:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Wraithmanilian
0

How's this:

<?php

$arr1 = array('a','b');
$arr2 = array('c','d');
$arr3 = array('e');

$finalarr = array($arr1,$arr2,$arr3);

print_r($finalarr);

?>
0

I know, but I told that I wanna put this figure into file.
This is stored in the file:

Array( [a] => Array([b] => aa)[c] => Array([d] => Array([0] => e[1] => f))

I want to store it on file exactly appear below:(figure is important)

Array
(
    [a] => Array
        (
            [b] => e
        )

    [c] => Array
        (
            [d] => Array
                (
                    [0] => f
                    [1] => g
                )

        )

)
0

Do you mean the formatting? If that's the case, then use...

<?php

$arr1 = array('a','b');
$arr2 = array('c','d');
$arr3 = array('e');

$finalarr = array($arr1,$arr2,$arr3);

$lines = print_r($finalarr);
$filename = 'test.txt';

if (is_writable($filename)) {

   if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
   }

   if (fwrite($handle, $lines) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
}

?>

If you're wanting it to look like this as screen output, then:

<?php

$arr1 = array('a','b');
$arr2 = array('c','d');
$arr3 = array('e');

$finalarr = array($arr1,$arr2,$arr3);

$lines = print_r($finalarr);
?>
<pre><?php echo $lines; ?></pre>

Another 2¢. Hopes this helps more. :)

0

Do you mean the formatting? If that's the case, then use...

<?php

$arr1 = array('a','b');
$arr2 = array('c','d');
$arr3 = array('e');

$finalarr = array($arr1,$arr2,$arr3);

$lines = print_r($finalarr);
$filename = 'test.txt';

if (is_writable($filename)) {

   if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
   }

   if (fwrite($handle, $lines) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
}

?>

If you're wanting it to look like this as screen output, then:

<?php

$arr1 = array('a','b');
$arr2 = array('c','d');
$arr3 = array('e');

$finalarr = array($arr1,$arr2,$arr3);

$lines = print_r($finalarr);
?>
<pre><?php echo $lines; ?></pre>

Another 2¢. Hopes this helps more. :)

Did you test your code?
$line = print_r($finalarr); will out "1"

0

Did you test your code?
$line = print_r($finalarr); will out "1"

Sorry about that, lack of sleep. :)

<?php

$arr1 = array('a','b');
$arr2 = array('c','d');
$arr3 = array('e');

$finalarr = array($arr1,$arr2,$arr3);

?>
<pre><?php print_r($finalarr); ?></pre>

Tested. 2¢ more. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.