note 78179 added to function.print-r
| From: | toader_alexandru at yahoo dot com | Date: | Tue, 02 Oct 2007 09:04:40 +0000 |
| Subject: | note 78179 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130976@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 86.35.136.59
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/78179
Del: integrated -- https://master.php.net/note/delete/78179/integrated
Del: useless -- https://master.php.net/note/delete/78179/useless
Del: bad code -- https://master.php.net/note/delete/78179/bad+code
Del: spam -- https://master.php.net/note/delete/78179/spam
Del: non-english -- https://master.php.net/note/delete/78179/non-english
Del: in docs -- https://master.php.net/note/delete/78179/in+docs
Del: other reasons-- https://master.php.net/note/delete/78179
Reject -- https://master.php.net/note/reject/78179
Search -- https://master.php.net/manage/user-notes.php