note 78179 deleted from function.print-r by ezyang
| From: | ezyang@php.net | Date: | Wed, 03 Oct 2007 01:28:34 +0000 |
| Subject: | note 78179 deleted from function.print-r by ezyang | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131015@lists.php.net to get a copy of this message | ||
Note Submitter: toader_alexandru at yahoo dot com
----
Hi
I wrote this class that returns the array in a form that can be used to create hard coded arrays.
You might not need this too often...
I needed it when I had to obtain a large array and the process took to long so for testing I get the
array once and then I just hardcoded it.
Is not perfect but it works for me great.
you make:
$x = new Print_array_s($msg);
$y = $x->getResp();
echo $y;
<?php
class Print_array_s {
var $tab = " ";
var $tabNumber = 0;
var $newLine = "\n";
var $result = '';
public function Print_array_s ($msg)
{
$this->parse_msg($msg);
}
public function getResp()
{
return $this->result;
}
private function parse_msg($msg)
{
if (is_array($msg)) {
$tabs = $this->makeTabs();
$this->result .= $this->newLine . $tabs . 'Array(' . $this->newLine;
$this->tabNumber++;
$tabs = $this->makeTabs();
$index = 0;
foreach ($msg as $i => $elem) {
if (is_string($i)) {
$this->result .= $tabs . "'" . $i . "'" .
' => ';
} else {//int
$this->result .= $tabs . $i . ' => ';
}
if (is_array($elem)) {
$this->result .= $this->parse_msg($elem);
} else {
if (is_numeric($elem)) {
$this->result .= $elem;
} elseif (is_string($elem)) {//string
$this->result .= '\'' . $elem . '\'';
} elseif (empty($elem)) {//null
$this->result .= '\'\'';
} else {//bool
$this->result .= $elem;
}
if ($index == count($msg) - 1) {//last elem in array
$this->result .= $this->newLine;
} else {
$this->result .= ',' . $this->newLine;
}
}
$index++;
}
$this->tabNumber--;
$tabs = $this->makeTabs();
$this->result .= $this->newLine . $tabs . ')' . $this->newLine;
}
}
private function makeTabs()
{
$tabs = null;
for ($i = 0; $i < $this->tabNumber; $i++) {
$tabs .= $this->tab;
}
return $tabs;
}
}
?>