note 56053 added to function.print-r

From: Date: Mon, 22 Aug 2005 08:38:20 +0000
Subject: note 56053 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-94017@lists.php.net to get a copy of this message
I needed to write generic code and I took the nice print_array function from 05-Jul-2005 03:56 and tuned it a bit. Single quotes are escaped now and tabs and new lines have been replaced by a new line after each array element on level 0. If you don't want any new lines just start with $php_code = get_array_php_code($my_array, 1); function get_array_php_code(&$array, $level = 0) { $php_code = ''; if(is_array($array)) { $php_code .= 'Array('; $ct = 1; foreach($array as $key => $value) { $php_code .= (is_numeric($key) ? $key : "'$key'" ) . '=>'; $php_code .= get_array_php_code($value, $level+1); if ($ct != sizeof($array)) { $php_code .= ','; if($level == 0) $php_code .= "\n"; } ++$ct; } $php_code .= ")"; } else if(is_numeric($array)) { $php_code .= $array; } else if(is_bool($array)) { $php_code .= $array ? 'TRUE' : 'FALSE'; } else { $php_code .= "'".str_replace("'","\\'", $array). "'"; } return $php_code; } With this function you can easily search and replace existing data arrays in other PHP scripts easily with substr_replace i.e. ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56053 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56053&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56053&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56053&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56053&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56053&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56053&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56053&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94017) next »